Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBScript to convert DD/MM/YYYY format to MM/DD/YYYY 1

Status
Not open for further replies.

sygg13

Programmer
Sep 4, 2012
7
US
Hi,

Let's suppose I have the following code:

HTML:
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/vbscript">


Dim aDate
Dim dateString, country

country = "UK"
dateString = "1/4/10"
if country = "UK" then
aDate = CDate(dateString)
document.write(aDate & "<br>")
document.write(FormatDateTime(aDate,1) & "<br />")
end if


</script>
</body>
</html>

The output generated reads:

1/4/2010
Monday, January 04, 2010


but, what I want is:

1/4/2010
*DayOfWeek*, April 01, 2010


Does VBScript have "locale support"? What's the best way for me to get the output I want?
 
As far as I can tell, there is no EDIT function on this site, so let me clarify by saying that the value for dateString is written via the UK format and is supposed to represent "April 1, 2010".
 
hi,

Is is ASSUMED by the VBS CDate() function that the supplied date STRING using yyyy and mm and dd structure, is either yyyy/mm/dd or mm/dd/yyyy.

Your date STRING is NOT in either of these expected structures. Therefore, you will need to parse the day, month and year and I'd then use the DateSerial() function to assign a DATE variable value.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
OK, so what I wound up doing was this. I'm not sure how to parse by using the "/" characters as markers, so I used the following instead:

HTML:
Dim aDate
Dim dateString, country
Dim switchedDay, switchedMonth, switchedYear

country = "UK"
document.write("UK<br>")
dateString = "1/4/10"  'represents April 1, 2010 in UK    'represents January 1, 2010 in US
aDate = CDate(dateString)
document.write("aDate before modification is" & FormatDateTime(aDate,1) & "<br>")

if country = "UK" then

switchedDay = DatePart("m", aDate) 
document.write("switchedDay is " & switchedDay & "<br>")
switchedMonth = DatePart("d", aDate)
document.write("switchedMonth is " & switchedMonth & "<br>")
switchedYear = DatePart("yyyy", aDate)
document.write("switchedYear is " & switchedYear & "<br>")

aDate =  switchedMonth & "/" & switchedDay & "/" & switchedYear'Right(switchedYear, 2) 'Check if YYYY or YY format matters in registration

document.write(aDate & "<br>")
document.write(FormatDateTime(aDate,1) & "<br />")

else

document.write(aDate & "<br>")
document.write(FormatDateTime(aDate,1) & "<br />")

end if
 
Using "1/4/10" as a test, it seems that the "10" is recognized as representing 2010.
 
I'd use the Split() function to create and array of the year, month, day.
Code:
dateString = "1/4/10"
a = split(dateString, "/")
aDate = DateSerial("20" & a(2), a(1), a(0))


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
In addition to SkipVought's elegant solution:

Code:
aDate = day(date()) & "/" & month(date) & "/" & year(date)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Sorry Geates, I believe that VB ASSUMES MONTH/DAY/YEAR; so "1/4/2012" will be converted to Jan 4, same problem as previously stated!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I'm not following. The only distinction I see is that the date can be specified as a string in your example. Am I missing something?

Code:
aDate = day(date()) & "/" & month(date) & "/" & year(date)
msgbox formatDateTime(aDate ,1)
'Wednesday, May 9, 2012

dateString = "9/5/12"
a = split(dateString, "/")
aDate = DateSerial("20" & a(2), a(1), a(0)) 
msgbox formatDateTime(aDate,1)
'Wednesday, May 9, 2012

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
@Geates,
Code:
    Dim adate As Date
    adate = Day(Date) & "/" & Month(Date) & "/" & Year(Date)
    Debug.Print adate, Format(adate, "mmm-dd")
result:
[tt]
5/9/2012 May-09

[/tt]
How does THAT work for you???

Rather...
Code:
    Dim dateString As String, a, adate As Date
    dateString = "5/9/12"  '[b] 5 sep 2012[/b]
    a = Split(dateString, "/")
    adate = DateSerial("20" & a(2), a(1), a(0))
    Debug.Print adate, Format(adate, "mmm-dd")
result:
[tt]
9/5/2012 Sep-05

[/tt]


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
It doesn't work.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
And what does "It doesn't work" mean?

You pushed the button and absolutely NOTHING happened?

You pushed the button and SOMETHING happened, but not what you expected?

You pushed the button and your PC exploded?

You pushed the button and something else did or didn't happen?

In any case, what was IT that did not WORK, and in what way?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've used Skip's parser, and that works fine. There's a lot of commented out lines in my code, so please ignore them.

Code:
dateString = "4/5/12"
dateString2 = "12/5/12"
RegSource = "UK"
response.write "RegSource is" & RegSource & "<br>" 

formattedDateString = ""
formattedDateString2 = ""

splitArray = split(dateString, "/")
if dateString2 <> "" then
splitArray2 = split(dateString2, "/")
end if


if RegSource = "UK" then

startDate = DateSerial("20" & splitArray(2), splitArray(1), splitArray(0))


	startDate2 = DateSerial("20" & splitArray2(2), splitArray2(1), splitArray2(0))

else
startDate = DateSerial("20" & splitArray(2), splitArray(0), splitArray(1))
	


	startDate2 = DateSerial("20" & splitArray2(2), splitArray2(0), splitArray2(1))
	
end if

formattedDateString = FormatDateTime(startDate,2)
formattedDateString2 = FormatDateTime(startDate2,2)

The end results for the formattedDateString variables are as desired. The month/day are flipped if "UK" is passed, and if "UK" is not passed, then the dates wind up the same as they started.
 
The goal of this post was to convert "1/4/12" (which represented April 1, 2012 in UK), into "4/1/12" (April 1, 2012 in US format). I need US format dates for my system.
 
Skip - the code you posted was VBA, not VBS. Therefore, "It didn't work".

sygg13 - In VBS, how a date is interpreted is based on what is specified in your computer's regional settings. In VBA, a date is assumed to be month/day/year.

- Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
@Geates, you are probably correct. I prefer to assume as little as possible and rather use unambiguous techniques, such as the yyyy/mm/dd structure for conversion.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top