2

I have one variable

Dim tt="2008-10-20 10:00:00.0000000"

I want to change it into date,

Matt
  • 14,906
  • 27
  • 99
  • 149
jain ruchi
  • 245
  • 3
  • 5
  • 11

6 Answers6

5

Try CDATE(tt) see http://www.w3schools.com/vbscript/func_cdate.asp. I used

vbscript cdate

as keywords at Google. There were more results.

Edit: Based on the comment below (I'm sorry for mixing up), using

FormatDateTime(date,format) 

Format contains following constants:

  • 0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM.
  • 1 = vbLongDate - Returns date: weekday, monthname, year
  • 2 = vbShortDate - Returns date: mm/dd/yy
  • 3 = vbLongTime - Returns time: hh:mm:ss PM/AM
  • 4 = vbShortTime - Return time: hh:mm

(copied from http://www.w3schools.com/vbscript/func_formatdatetime.asp)

John Hascall
  • 9,176
  • 6
  • 48
  • 72
Reporter
  • 3,897
  • 5
  • 33
  • 47
2

This link, (MS CDate page), explains that:

adate = CDate(astring)

converts a string into a date object. For there, you can format it with the FormatDateTime function

str = FormatDateTime(Date)

the FormatDateTime function is "smart" -- it will format as date and time if both are present, otherwise it will format with whichever of date or time is present.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
John Hascall
  • 9,176
  • 6
  • 48
  • 72
2

I propose a safe solution which returns the result only if the conversion is successful:

s="2008-10-20 10:00:00.0000000"
On Error Resume Next
d=CDate(Left(s,19))
On Error Goto 0
if not IsEmpty(d) then MsgBox d

Try it for a non-valid date or non-valid format. The result will be empty.

s="2008-02-31 10:00:00"

In same contexts, it is necessary to initialize the variable collecting result of CData. I recommend to initialize it as Empty. Example below shows such case - counting valid dates in a string array:

Lines = array("2008-10-20 10:00:00.0000000", "2008-10-20 10:00:00", "", "2008-02-31", "Today", "2017-02-7")
On Error Resume Next
Count=0
for each Line in Lines
    d=Empty
    d=CDate(Line)
    if not IsEmpty(d) then Count=Count+1
next
On Error Goto 0
MsgBox "Number of valid dates is "&Count

The correct answer is 2. Without initialization we get 5 as the CDate does not do anything on error so variable keeps the value from a recent iteration in the loop.

Antoni
  • 342
  • 2
  • 7
1

I believe cdate is dependent on local settings to parse the string. This is no good in many situations.

To avoid this you need to use DateSerial()

and if needed add any time components to the result separately.

vikjon0
  • 182
  • 1
  • 12
1

If do not need your milliseconds, your could use the following:

<script type="text/vbscript">
    s="2008-10-20 10:00:00.0000000"
    arr= Split(s, ".")
    d=CDate(arr(0))
    document.write(d)
</script>
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
0

The date literal in classic asp is unreliable. If the first or second part is greater than 12, it takes that value for day, the other as month. If both parts are less than 12, the interpretation is unpredictable: sometimes american and sometimes british.

A work-around is to force the entry of dates into separate fields or use a date entry module which can set the date into british or american style.

A date literal should be treated as american and use a function to convert that into a date variable using Dateserial().

function amerdate(str)
  'str 3/5/2023 form: in american format: use for calculations including date
  Dim d 
  d = Split(str, "/")
  amerdate = Dateserial(d(2), d(0), d(1))
end function

Use only american date in calculations like Dateadd() etc.

someday = "3/5/2023" '5th march, american date literal
nextday = Dateadd("d", 1, amerdate(someday))

Whenever a date display is required, convert to british date and show it.

function britdate(str)
  'str: 3/5/2023 form: display in british form. not for calculations
  Dim d 
  d= Split(str, "/")
  britdate = d(1) & "/" & d(0) & "/" & d(2)
end function
Polymath
  • 630
  • 7
  • 11