Creating a Date string

November 11th, 2009 - 12:28 am ET by Webbiz | Report spam
Suppose you know the Month, Day and Year as separate values
(variables).

You want to combine them into a complete date string based on the
Regional Date format.

You know what the format is, because you have it in your variable
called sRegionalDateFormat by using the GetRegionalDateFormat
function.

Using all this information, what would be the best way to create this
date string?

Thanks.

Webbiz
email Follow the discussionReplies 6 repliesReplies Make a reply

Similar topics

Replies

#1 Nobody
November 11th, 2009 - 01:51 am ET | Report spam
"Webbiz" wrote in message
news:
Suppose you know the Month, Day and Year as separate values
(variables).

You want to combine them into a complete date string based on the
Regional Date format.

You know what the format is, because you have it in your variable
called sRegionalDateFormat by using the GetRegionalDateFormat
function.

Using all this information, what would be the best way to create this
date string?



You don't need to know the date format, just use CStr() on any date, this
uses the short date format in Control Panel. Otherwise use the Format()
function. As for turning Month, Day, Year to Date, use this code:

Dim d As Date

d = DateSerial(y, m, d)

Searching the web for GetRegionalDateFormat() shows code that use
GetProfileString() to read information from Win.ini. It's better to use
GetLocaleInfo() instead. See this VB6 sample:

http://vbnet.mvps.org/code/locale/localedates.htm
Replies Reply to this message
#2 Rick Rothstein
November 11th, 2009 - 04:00 am ET | Report spam
Here is a non-API function that will return the date format...

Function DateFormat() As String
DateFormat = CStr(DateSerial(2003, 1, 2))
DateFormat = Replace(DateFormat, "2003", "YYYY")
DateFormat = Replace(DateFormat, "03", "YY")
DateFormat = Replace(DateFormat, "01", "MM")
DateFormat = Replace(DateFormat, "1", "M")
DateFormat = Replace(DateFormat, "02", "dd")
DateFormat = Replace(DateFormat, "2", "d")
DateFormat = Replace(DateFormat, MonthName(1), "MMMM")
DateFormat = Replace(DateFormat, MonthName(1, True), "MMM")
End Function

And for those who want the one-liner version of this function<g>...

Function DateFormat() As String
DateFormat = Replace(Replace(Replace(Replace(Replace(Replace(Replace( _
Replace(CStr(DateSerial(2003, 1, 2)), "2003", "YYYY"), _
"03", "YY"), "01", "MM"), "1", "M"), "02", "dd"), "2", _
"d"), MonthName(1), "MMMM"), MonthName(1, True), "MMM")
End Function

Rick (MVP - Excel)


"Nobody" wrote in message
news:%
"Webbiz" wrote in message
news:
Suppose you know the Month, Day and Year as separate values
(variables).

You want to combine them into a complete date string based on the
Regional Date format.

You know what the format is, because you have it in your variable
called sRegionalDateFormat by using the GetRegionalDateFormat
function.

Using all this information, what would be the best way to create this
date string?



You don't need to know the date format, just use CStr() on any date, this
uses the short date format in Control Panel. Otherwise use the Format()
function. As for turning Month, Day, Year to Date, use this code:

Dim d As Date

d = DateSerial(y, m, d)

Searching the web for GetRegionalDateFormat() shows code that use
GetProfileString() to read information from Win.ini. It's better to use
GetLocaleInfo() instead. See this VB6 sample:

http://vbnet.mvps.org/code/locale/localedates.htm




Replies Reply to this message
#3 Nobody
November 11th, 2009 - 09:22 am ET | Report spam
"Nobody" wrote in message
news:%
Dim d As Date

d = DateSerial(y, m, d)



Also, if you want time too, you can use this:

d = DateSerial(y, m, d) + TimeSerial(iHour, iMinute, iSecond)
Replies Reply to this message
#4 Webbiz
November 11th, 2009 - 12:33 pm ET | Report spam
On Wed, 11 Nov 2009 01:51:22 -0500, "Nobody"
wrote:

"Webbiz" wrote in message
news:
Suppose you know the Month, Day and Year as separate values
(variables).

You want to combine them into a complete date string based on the
Regional Date format.

You know what the format is, because you have it in your variable
called sRegionalDateFormat by using the GetRegionalDateFormat
function.

Using all this information, what would be the best way to create this
date string?



You don't need to know the date format, just use CStr() on any date, this
uses the short date format in Control Panel. Otherwise use the Format()
function. As for turning Month, Day, Year to Date, use this code:

Dim d As Date

d = DateSerial(y, m, d)

Searching the web for GetRegionalDateFormat() shows code that use
GetProfileString() to read information from Win.ini. It's better to use
GetLocaleInfo() instead. See this VB6 sample:

http://vbnet.mvps.org/code/locale/localedates.htm





Thanks amigo!

Webbiz
Replies Reply to this message
#5 Webbiz
November 11th, 2009 - 12:35 pm ET | Report spam
On Wed, 11 Nov 2009 04:00:32 -0500, "Rick Rothstein"
wrote:

Here is a non-API function that will return the date format...

Function DateFormat() As String
DateFormat = CStr(DateSerial(2003, 1, 2))
DateFormat = Replace(DateFormat, "2003", "YYYY")
DateFormat = Replace(DateFormat, "03", "YY")
DateFormat = Replace(DateFormat, "01", "MM")
DateFormat = Replace(DateFormat, "1", "M")
DateFormat = Replace(DateFormat, "02", "dd")
DateFormat = Replace(DateFormat, "2", "d")
DateFormat = Replace(DateFormat, MonthName(1), "MMMM")
DateFormat = Replace(DateFormat, MonthName(1, True), "MMM")
End Function




Thanks Rick, I'll have to try this out.

And for those who want the one-liner version of this function<g>...

Function DateFormat() As String
DateFormat = Replace(Replace(Replace(Replace(Replace(Replace(Replace( _
Replace(CStr(DateSerial(2003, 1, 2)), "2003", "YYYY"), _
"03", "YY"), "01", "MM"), "1", "M"), "02", "dd"), "2", _
"d"), MonthName(1), "MMMM"), MonthName(1, True), "MMM")
End Function




You're a riot!

Cheers!

Webbiz
Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search