0

I'm getting "August 22, 2015 10:55" in a querystring and I want to convert it to "2015-08-22 10:55".

I have this so far:

 datum=request.querystring("tid")
 response.write datum ----this writes out August 22, 2015 10:55
 datum=Cstr(datum)

 DateVar = datevalue(datum)

 NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar) 
 NewTimeVar=timevalue(datum)

 newdatum = NewDateVar & " " & NewTimeVar

But now I get a type missmatch error on DateVar = datevalue(datum).

If I use datum="August 22, 2015 10:55" to test with, then it works.

ZygD
  • 22,092
  • 39
  • 79
  • 102
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • What have you tried so far? We obviously don't want to waste your time (or ours) by suggesting what you have tried already. Please be specific, and also note that without a *specific* problem your question may be deemed [off-topic](http://stackoverflow.com/help/on-topic) for stackoverflow. Many thanks. – Wai Ha Lee Jun 22 '15 at 13:06
  • It run in my server. You can try datum=Cstr(trim(datum)) – Fabio Pellerito Jun 22 '15 at 15:18
  • 1
    If you don't want to worry about unescaping spaces, colons, etc., you could pass the date as a Double instead (it's how dates are stored internally anyway). For example, `Response.Redirect "/page.asp?tid=" & CDbl(Now)`. Then just convert it back with `CDate(datum)`. Of course, if your region uses a comma for the decimal separator, you may need to account for that in your URI. – Bond Jun 22 '15 at 23:21

3 Answers3

1

To spell it out:

>> s = "August 22, 2015 10:55"
>> d = CDate(s)
>> WScript.Echo TypeName(d), d
>>
Date 22.08.2015 10:55:00 <-- german locale!

Added:

To get the required format, you can use the strategy described here.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • If I test with datum="August 22, 2015 10:55" then I got it working but if I get it from the query string, datum=request.querystring("tid") then I get a Type mismatch: 'datevalue' – Claes Gustavsson Jun 22 '15 at 14:05
  • @ClaesGustavsson - so check data type, content, and encoding of the querystring carefully. Obviously it is **not** like "August 22, 2015 10:55". (Why do you ignore my comment wrt Datevalue()?) – Ekkehard.Horner Jun 22 '15 at 14:09
  • If I write out the value from the query string I get "August 22, 2015 10:55"! No I did not ignore it, see my original post. – Claes Gustavsson Jun 22 '15 at 14:18
  • 1
    @ClaesGustavsson - the 'write' may well hide encoding of space by %20. – Ekkehard.Horner Jun 22 '15 at 14:28
  • Yes so I replaced it with: dat=replace(dat, "%20", " ") dat=replace(dat, "%3A", ":") And then I try to make sure its a string: dat=Cstr(dat) – Claes Gustavsson Jun 22 '15 at 14:34
  • @Ekkehard.Horner `Request.Querystring` decodes values for you so there should be no lingering encoded values like `%20` *(for space)* – user692942 Jun 24 '15 at 08:41
0
DateVar = datevalue("August 22, 2015 10:55")
NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar) & "-" & time(DateVar)
Fabio Pellerito
  • 204
  • 1
  • 9
  • Thanks a lot Fabio, but I get an error I don't understand? Wrong number of arguments or invalid property assignment: 'time' – Claes Gustavsson Jun 22 '15 at 13:17
  • 2
    As DateValue removes the time, you should use CDate (depending on the regonal setting/locale either or both will/won't 'work'). Time() - without params - will give the **current** time, did you mea TimeValue()? – Ekkehard.Horner Jun 22 '15 at 13:18
  • If I use NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar) then it displays the right date format! But if I try with: timeVar = timevalue("August 22, 2015 10:55") NewTimeVar=time(timeVar) I still get Wrong number of arguments or invalid property assignment: 'time' – Claes Gustavsson Jun 22 '15 at 13:30
  • @ClaesGustavsson `Time()` can only be used to return the current time it has no set operation. Instead you should be using `TimeValue()` and pass it a valid time string such as in this example `TimeValue("10:55")`. Because `Time()` is read only it doesn't expect a value to assign hence the error. – user692942 Jun 24 '15 at 12:03
0

Classic ASP tends to use US date formats, come hell or high water. However, it's easy enough to format the date you display (or pass on to the database) in whatever way you wish.

The following will pad any single-digit values with a 0. If you don't need that, then just use the plain functions, without Right().

Dim datum, mm, dd, yyyy, hh, nn, fdatum
datum = Request.Querystring("tid")
If IsDate(datum) Then
    datum = CDate(datum)
    mm = Right(100 + Month(datum),2)
    dd = Right(100 + Day(datum),2)
    yyyy = Year(datum)
    hh = Right(100 + Hour(datum),2)
    nn = Right(100 + Minute(datum),2)
    fdatum = yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn
Else
    fdatum = datum '- or whatever fallback value you want
End If
Martha
  • 3,932
  • 3
  • 33
  • 42
  • No it doesn't it uses the servers regional settings. If your default regional language is set to `English (United States)` you will get US date formats. Make sure the default is what you prefer on the server. What you are doing there is not required and will require a lot more validation. – user692942 Jun 24 '15 at 08:38
  • 1
    @Lankymart: check the docs for VBScript. There are all sorts of places where it uses US date format, no matter what the server is set to. – Martha Jun 24 '15 at 19:55
  • We are talking Classic ASP not plain VBScript. Date functions can all be controlled using `LCID`. – user692942 Jun 25 '15 at 00:10
  • @Marta Can you give me some examples of where? I have never had this problem and dates / date formatting has always been controlled by the regional settings of the underlying server operating system. – user692942 Jun 25 '15 at 09:04