Excel wont recognize date using openpyxl - excel

I have a piece of code that converts a unix date to standard U.S. date format. It displays properly in excel, however excel doesn't recognize it as a date.
if str(startdate).isdigit():
startdate = int(startdate)
date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y')
ws4.cell(row=cell.row,column=2).value = date
ws4.cell(row=cell.row,column=2).number_format = 'MM DD YYYY'
Any idea how to get excel to see this as a date rather than text?

My mistake was assuming the line below created a date.
date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y')
After venturing into the docs (Scary as hell for a noob. Does it get easier?) I realized .strftime('%m/%d/%Y') created a string not a date.
I converted that string to a date using:
date = datetime.datetime.strptime(date, '%m/%d/%Y').date()
Now excel recognizes it as a date.
Hopefully this helps someone in the future.

Related

Excel - How to convert US date string (dd-mmm-yyyy) to European date

How can date in the US format dd-mmm-yyyy be converted to a date in European format.
When importing text based data with US Dates, some dates will import as a date, some import as a string.
A US date like 11-Jun-2021 is equal to NL format, so it will import as a date.
A US date like 11-Oct-2021 should be converted to 10-okt-2021, but it's imported as a string.
Is there a way to convert this string to a date format?
Found something. It's kinda dirty, but it works:
=IF(C3=0;"";IF(IFERROR(FIND("Oct";C3;1);FALSE);REPLACE(C3;FIND("Oct";C3;1);3;"Okt");IF(IFERROR(FIND("May";C3;1);FALSE);REPLACE(C3;FIND("May";C3;1);3;"Mei");IF(IFERROR(FIND("Mar";C3;1);FALSE);REPLACE(C3;FIND("Mar";C3;1);3;"Mrt");C3))))
This works for me in Italian. A3 contains the original date (only 8 months need to be translated):
=IF(ISNUMBER(A3);A3;1*SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A3;"Jan";"GEN");"May";"MAG");"Jun";"GIU");"Jul";"LUG");"Aug";"AGO");"Sep";"SET");"Oct";"OTT");"Dec";"DIC"))

Date export from Matlab to excel

i have a set of data for couple of days and the names of the data files like this
name='Newyork20200915'
which is for the 15th of September and i want to export only the date to excel like shown below
So how can i get the date from the name string ?
Thanks in advance
Assuming that the other part of name will not contain any digits besides the date, you can use regexp to get all the digits from the character array:
name = 'Newyork20200915'
date_only = regexp(name, '\d*', 'match')
Next, you can convert this date string to a serial date number using datenum, by providing the format in which the date is currently. And then use datestr to format it to your desired format.
date_formatted = datestr(datenum(date_only, 'yyyymmdd'), 'dd. mmm')
date_formatted =
'15. Sep'

VBA Date + TimeValue returns no time

I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?
Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.
When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.

Date format in vba

I have VBA code which pull the details from .msg file (outlook files) and update in excel sheet. While reflecting the date column, it is showing as "3/9/2016 11:03:27 AM" but I want to show only date and not time.
I used the format option i.e.
Sheet2.Cells(Row + 1, 23) = VBA.Format(sentDate, "dd/MM/yyyy")
but it is showing the date a "8/3/2016 00:00". I want to reflecting only date and nothing else. Please guide me as to what all changes required to reflect only date.
Give this a try:
Sheet2.Cells(Row + 1, 23).Value = sentDate
Sheet2.Cells(Row + 1, 23).NumberFormat = "mm/dd/yyyy;#"
Yet, I have a feeling that the cell does not really contain a date but merely text. So, please also try to change the .NumberFormat to the following first:
Sheet2.Cells(Row + 1, 23).NumberFormat = "General"
If the dates are now showing all as numbers then these are actual dates and the first proposal should work. If not, then these are not dates yet and you'll have to convert the text (which looks like dates) to dates first.
For more information you might want to read this: Difference between date and time w/out work week Excel

VBA dates collected correctly but output wrongly

I grab dates from one spreadsheet and output them onto another spreadsheet. After grabbing the date, when I debug.print it is in the correct format. When I output the date, debug.print also displays the correct format. However, the format on the spreadsheet the value has just been sent to, doesnt show the correct format.
I am using:
Sheets(SheetName).Cells(RowNum, ColNum).Value = Data
Sheets(SheetName).Cells(RowNum, ColNum).NumberFormat = "dd/mm/yyyy"
after I have pasted the value, but the months and days are still switched the wrong way.
Is there something I am doing wrong?? If I right click the cell it thinks it's date is dd/mm/yyyy but instead of 4th Sept it is showing 9th April.
This might be some trouble with localization:
Try using NumberFormatLocal, if DanielCooks tip didn't help ;)
edit: erlier it was statet by mister Cook, to check if the given data is correct.
edit:
With my german version I have quite some trouble to use / as the seperator, that is why i tryied with this code .NumberFormat ="dd-mm-yyyy;#" - works fine; I can switch days and month as I like.
edit:
With .NumberFormatLocal = "TT/MM/JJJJ" I have to use the german shorts for day, month and year, but now I can use / as the seperator.
You should experiment a litte bit with some formats strings ;)
Sorry to resurrect an old post, however I had a problem with VBA outputting a valid date as US style with the actual date changed for example 1st May changed to 5th Jan. I came upon this post but I didn't find the answer I needed here and now that I have worked it out I thought I would share it:
The key is not to define the variable storing the date as a "date" but as a "double", e.g.
Dim ReportDate As Double
ReportDate = Date
Range("E6").Value = ReportDate
This works as it outputs the numeric "date value" which excel then formats locally e.g. 41644 gets formatted as "05/01/14" using UK format or "01/05/14" using US format.
Hope this proves useful to other people (probably me when I forget how I solved it in six months time).
In the end I had to format the cell as "TEXT" to keep the correct format
(1) You need to define the variable to "Date" type to read the input, then set the date format before assigning it to the date variable.
(2) You also need to format the date output to make it work !
'**Read the date input****
Dim date1 as Date
Range("B2").NumberFormatLocal = "dd/mm/yyyy"
date1 = Range("B2").Value
'**Output the date****
Range("E2").Value = date1
Range("E2").NumberFormatLocal = "dd/mm/yyyy"

Resources