Change string into date (1 Viewer)

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
Hello,
I am trying to convert this string : Feb 18, 2020 20:00
into this date in this format : 18/02/2020 20:00

I guess I can get day and year easily with the mid function, but what about the month ?
thanks
 

isladogs

MVP / VIP
Local time
Today, 18:48
Joined
Jan 14, 2017
Messages
18,186
From the immediate window
Code:
?Format(#Feb 18, 2020 20:00#,"dd/mm/yyyy hh:nn")
18/02/2020 20:00

Note that using Format creates a string value
 

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
thanks a lot
It works like a charm
 

isladogs

MVP / VIP
Local time
Today, 18:48
Joined
Jan 14, 2017
Messages
18,186
You're welcome.
If you need it to be a date rather than a string, you can just do this:

Code:
?#Feb 18, 2020 20:00#
18/02/2020 20:00:00

It will be displayed in your local date format
 

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
hi again,
i was trying to incorporate it in my code, but it seems it´s not posible to pass that string as a variable

fj = "Feb 18, 2020 20:00"
fullDataTime = Format(fj,"dd/mm/yyyy hh:nn")
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:48
Joined
Sep 21, 2011
Messages
14,046
hi again,
i was trying to incorporate it in my code, but it seems it´s not posible to pass that string as a variable

Works for me?:confused:

From the immediate window
Code:
fj = "Feb 18, 2020 20:00"
fullDataTime = Format(fj,"dd/mm/yyyy hh:nn")
? fulldatatime
18/02/2020 20:00

As isladogs mentioned it will still be a string. If you want an actual date also use the CDate() function.?

HTH
 

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
Gasman,
This is very strange :

I have the variable fj defined as a string, and from the inmediate window I get :

Code:
?fj
Feb 18, 2020 20:00

?Format(fj,"dd/mm/yyyy hh:nn")
Feb 18, 2020 20:00
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:48
Joined
Sep 21, 2011
Messages
14,046
Gasman,
This is very strange :

I have the variable fj defined as a string, and from the inmediate window I get :

Code:
?fj
Feb 18, 2020 20:00

?Format(fj,"dd/mm/yyyy hh:nn")
Feb 18, 2020 20:00

Not sure what is going on then? Would that be your default date format by any chance?
I even tried mm/dd/yyyy and that worked for me.:confused:
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 18:48
Joined
Sep 12, 2006
Messages
15,614
try datevar = cdate(date_string)

the problem is that bad formatting might produce an error - i'm not sure - it might even reject an extra space between the date and the time.
 

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
try datevar = cdate(date_string)

that does not work either returning a type mismatch error , but it pointed me into the right direction.I forgot to tell you that I am from Portugal, and here the month of February abreviates to "Fev" (Fevereiro)

so, if the variable is like the following, it works correctly :
Code:
fj = "Fev 18, 2020 20:00"

so now and before I pass the date string to fj , I will have to rename the month abreviation to portuguese, unless there is another way.

Thanks a lot
 

isladogs

MVP / VIP
Local time
Today, 18:48
Joined
Jan 14, 2017
Messages
18,186
Didn't you notice that I used date delimiters ## in both my replies?
 

cpampas

Registered User.
Local time
Today, 11:48
Joined
Jul 23, 2012
Messages
218
so, I guess there is no way to replicate the use of date delimeters in a variable , and afterwards to format the string

Format(fj,"dd/mm/yyyy hh:nn")
 

isladogs

MVP / VIP
Local time
Today, 18:48
Joined
Jan 14, 2017
Messages
18,186
Hi Paul
The question was really intended for the OP....;)

For info, whilst this also works
Code:
?cdate(#Feb 18, 2020 20:00#)
18/02/2020 20:00:00
If you omit the two ##, it gives a compile error.

And, as already mentioned, the CDate isn't actually required here
Code:
?#Feb 18, 2020 20:00#
18/02/2020 20:00:00
 

isladogs

MVP / VIP
Local time
Today, 18:48
Joined
Jan 14, 2017
Messages
18,186
Cpampas

Code:
Dim fj As String, fd As String
fj = "Feb 18, 2020 20:00"

fd = Format(fj, "dd/mm/yyyy hh:nn")

Debug.Print fj,
Debug.Print fd

Result:
Feb 18, 2020 20:00
18/02/2020 20:00
 

Users who are viewing this thread

Top Bottom