livior
livior

Reputation: 33

"format" doesn't work with a specific date format

I can format a date in a specific date format, but the formatted string seems not possible to format anymore. It wouldn't be a problem, because I could save the old format. But the thing is that I also have to read a string in the specific format from an excel cell and format it.

I use the german date system. (Mi = Wednesday)

UI_Main.txt_BeginnDatum.Value = Format(UI_Date_Picker.Date_Picker.Value, "ddd dd mmm yyyy")

That's how I get the date from the datepicker. It's now formatted in the specific date format I'm talking about (Mi. 09 Jan 2019). The default format from the picker is dd.mm.yyyy.

strBeginDate_g = txt_BeginnDatum.Value

strTemp = strTemp & "Nr. " & Format(strBeginDate_g, "yyyy-mm-dd")

Here I try to write the date in another format, but the output is just the same as before.

Of course I could write my own function but I am sure format is supposed to handle this.

Upvotes: 0

Views: 166

Answers (2)

Nathan_Sav
Nathan_Sav

Reputation: 8531

You could write a custom format, like so

Function FormatYYYYMMDD(strDateIn As String) As Date

Dim a() As String
Dim s As String

a = Split(strDateIn, Chr(32))
a(0) = vbNullString

FormatYYYYMMDD = CDate(Format(a(1) & "/" & a(2) & "/" & a(3), "yyyy-mm-dd"))

Erase a

End Function

Upvotes: 1

Pᴇʜ
Pᴇʜ

Reputation: 57743

The Format function can only format a numeric value. But you give it a string strBeginDate_g as parameter.

Instead give it the value as parameter:

strTemp = strTemp & "Nr. " & Format$(UI_Date_Picker.Date_Picker.Value, "yyyy-mm-dd")

Once you formatted a date with Format() it becomes a string, and you cannot calculate anymore with a string nor can you format it again.

Upvotes: 1

Related Questions