Change string into date (1 Viewer)

cpampas

Registered User
Joined
Jul 23, 2012
Messages
28
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

CID Moderator
Staff member
Joined
Jan 14, 2017
Messages
12,326
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
Joined
Jul 23, 2012
Messages
28
thanks a lot
It works like a charm
 

isladogs

CID Moderator
Staff member
Joined
Jan 14, 2017
Messages
12,326
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
Joined
Jul 23, 2012
Messages
28
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
Joined
Sep 21, 2011
Messages
5,016
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
Joined
Jul 23, 2012
Messages
28
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
Joined
Sep 21, 2011
Messages
5,016
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
Joined
Sep 12, 2006
Messages
13,873
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
Joined
Jul 23, 2012
Messages
28
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

CID Moderator
Staff member
Joined
Jan 14, 2017
Messages
12,326
Didn't you notice that I used date delimiters ## in both my replies?
 

cpampas

Registered User
Joined
Jul 23, 2012
Messages
28
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

CID Moderator
Staff member
Joined
Jan 14, 2017
Messages
12,326
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

CID Moderator
Staff member
Joined
Jan 14, 2017
Messages
12,326
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 (Users: 0, Guests: 1)

Top Bottom