How to convert string to datetime format classic asp
Asked Answered
L

6

2

I have one variable

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

I want to change it into date,

Lashawnda answered 3/1, 2012 at 9:44 Comment(0)
L
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)

Lunatic answered 3/1, 2012 at 9:54 Comment(4)
Thanks for your replay, but i want to change it into datetime not only date.Lashawnda
Note: link appears to be dead (and it is generally held the w3schools links are often less valuable than others)Con
@JohnHascall Look at the date. and look at the last comment in postLunatic
In my opinion this doesn't address the problem. The request is to convert string to date using a specific format for the date.Morsel
C
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.

Con answered 31/1, 2016 at 15:46 Comment(0)
D
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.

Dressmaker answered 7/2, 2017 at 10:7 Comment(0)
E
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>
Elysian answered 3/1, 2012 at 10:20 Comment(0)
B
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.

Brushwork answered 31/1, 2016 at 15:17 Comment(1)
True. See my answer for a sample using Dateserial()Salubrious
S
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
Salubrious answered 15/1, 2023 at 12:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.