Excel showing wrong date time format - excel

I am using this below code
Range("A1") = Format$(Now, "dd/mm/yyyy h:mm:ss AM/PM")
and the o/p as shown in A1 is 02/12/2020 20:08:38 which is incorrect as today is 12th Feb 2020, so it should be 12/02/2020 20:08:38. Just the dd is interchanged with mm and there is no AM/PM. Already have checked the control panel--region and it's English (United Kingdom). OS is Win10. My system shows 12/02/2020 as the date. When I write 12/02/2020 manually it accepts as it is and doesn't change it.
Don't have any clue about it. Thanks for any help.

Instead of using Format$ (which returns a String anyway, not an actual datetime), change the NumberFormat of the cell:
Range("A1").NumberFormat = "dd/mm/yyyy h:mm:ss AM/PM"
Range("A1").Value = Now

Related

How do I convert Excel General Text to Date

So my problem is I have a Date that is defined as General in Excel as this "10 JUL 2021 10:30" I want to make it Excel Date.
kindly have a look at my picture for detailed understanding.
Need any kind of solution to automate this VBA or any formula,
Thanks in advance
If you need VBA to extract Date, please use the next function:
Function TextToDateX(txt As String) As Date
Dim arrD, arrM
arrM = Split("JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC", ",")
arrD = Split(txt, " ")
TextToDateX = DateSerial(CLng(arrD(2)), Application.match(arrD(1), arrM, 0), CLng(arrD(0))) + CVDate(arrD(3))
End Function
It can be tested as:
Sub testTextToDateX()
Dim txt As String, d As Date
txt = "10 JUL 2021 10:30"
d = TextToDateX(txt)
Debug.Print Format(d, "dd/mm/yyyy hh:mm")
End Sub
DateValue is localization related. Microsoft states: "DateValue recognizes the order for month, day, and year according to the Short Date format that you specified for your system". So, it may work or not.
In my case it doesn't...
I just tried using the DateValue() worksheet function, and every worked fine (normal Excel formula, no VBA needed):
=DATEVALUE("07 JUL 2021 10:30")
One thing that might be interesting for you: there exists a cell formatting (dd mmm yyyy hh:mm), which you seem to be using. (When you apply this on any cell, the value inside gets automatically formatted into that kind of date format.)
Edit, based on comment:
In case there's a comma in your date string, remove it first and then apply the DateValue() function:
=DATEVALUE(SUBSTITUTE("07 JUL 2021, 10:30",",",""))

excel vba writing dates dd/mm/yyyy

I am creating a report page for my excel template and I want to add a "date" section below.
I simply wrote the code as this;
Worksheets("Report_TEMP").Range("H46:J46") = Format(Date, "dd-mm-yyyy")
I don't know why, but the report gives me the date as "mm-dd-yyyy".
It show today as 10.12.2017 like US system.
How can I make it sure to write it as 12.10.2017 ?
You should apply the formatting to the cells:
With Worksheets("Report_TEMP").Range("H46:J46")
.NumberFormat = "dd-mm-yyyy"
.Value = Date
End With
You can use the format as DD.MM.YYYY as mentioned below
Worksheets("Report_TEMP").Range("H46:J46") = Format(Date, "dd.mm.yyyy")
It will give us the desired output. Thanks
you can use as a string, like this :
Worksheets("Report_TEMP").Range("H46:J46") = Cstr(Format(Date, "dd-mm-yyyy"))

How to change date format to Month (abbreviated text)

how do i change formate date from 25-Feb-2013 to Feb only?
Means here, it only extract the abbreviated month only.
i try to use this code but it does't work and it turn Feb to Jul
Sheet1.Range("A2:A9") = Format(Date, "mmm")
Sheet1.Range("A2:A9") = Application.text(Date, "mmm")
You may try:
Sheet1.Range("A2:A9").NumberFormatLocal = "mmm"
As suggested by #user3271518, this is found by recording a macro, changing the format and then looking at the macro recorded.

Making Excel Recognize Custom Day/Time Format

I'm working with a date and time format from the Twitter API. It looks like this:
Tue Nov 26 20:44:15 +0000 2013
Is there a formula to convert this to a format that could be sorted chronologically? I don't need the +0000. Also not concerned about the day of week.
=DATEVALUE(MID(A1,9,3) & MID(A1,4,5) & RIGHT(A1,4)) + TIMEVALUE(MID(A1,11,9))
Then format as you like. As requested you would want a Custom Format of mmm dd HH:mm yyyy
Try this, assuming that your string is in A1:
=DATETIME(MID(A1, 5, 16) & MID(A1, 27, 4))
The two MID formulas cut out the parts of the string you want, namely the month, day, time, and year, but exclude the day of the week and the timezone. This produces a string that the DATETIME function can automatically recognize and covert into a native Excel date format.

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