1

I want to convert following date into GMT date. Please anyone can tell me how to do it. Any help would be highly appreciated.

Update

Hey I found this

   <% response.write currentUTC() %>

   <script language=jscript runat=server>
        function currentUTC(){
        var d, s;
       d = new Date();
       s = "Server current UTC time is: ";
       s += d.toUTCString('!%a, %d %b %Y %H:%M:%S GMT');
       return(s);
      }
 </script>

It outputs : Server current UTC time is: Fri, 15 Jan 2016 07:42:13 UTC

But I need in this format: YYYYMMDDHHMMSS

Please anyone?

Update

I tried to use below function:

GetServerGMT=Year(Now())&Month(Now())&Day(Now())&Hour(Now())&Minute(Now())&Second(Now())&WeekDay(Now()) 

It outputs: 20161172035121

But this is not valid timestamp.

14
  • It depends what it is to start with in terms of timezone offset.
    – user692942
    Jan 12, 2016 at 10:58
  • Its classic asp, I have displayed gmt but not this one. Would you please show me with example. thanks
    – Blue Rose
    Jan 12, 2016 at 10:59
  • Are you using ASP.Net or Classic ASP (there is a big difference). You've tagged it as Classic ASP but specify ASP.Net in the question title..
    – user692942
    Jan 12, 2016 at 10:59
  • It should help you play around with Date() in VBScript - stackoverflow.com/a/22575530/692942 Post your code attempts and I'll try and help.
    – user692942
    Jan 12, 2016 at 11:01
  • 1
    Duplicate of this question - stackoverflow.com/questions/11398084/…
    – John
    Jan 12, 2016 at 11:56

2 Answers 2

0

Adding to what Lankymart linked. ASP Classic has many time / date options and from my experience it seems you have to create a function or sub for every different type.

My HTTP Only cookie GMT is different from my GMT RSS Feed layout.

Example 1: strGMTDateRFC22 = CookieServerUTC("d",1,5,"GMT")

'# following formating RFC22 for your GMT Cookie time. 
    strGMTDateRFC22 = CookieServerUTC("d","&strCookieExpires&",5,"GMT")  ' 1 Day set in char enc dec page
    Response.AddHeader "Set-Cookie", strCookieName & "=" & strCookieKey & "=" & strCookieValue & "; expires=" & strGMTDateRFC22 & "; domain="& strCookieDomain &"; path=/; HTTPOnly"

First of two functions

    Function CookieServerUTC(strD,strT,strOSet,strZ)
Dim strTG,strCookieD
'snipped unwanted code
        strTG = DateAdd("h", strOSet,Now())
        strCookieD = DateAdd(strD,strT,CDate(strTG))
     CookieServerUTC =  fncFmtDate(strCookieD, "%a, %d %b %Y %H:%N:%S "&strZ&"")

    End Function

Another example when you need to setup Server UTC This is allows for strH = "h" strT = "5" (strT Time Offset +/-) and strZ GMT (Timezone)

Function GetServerUTC(strH,strT,strZ)

 GetServerUTC = fncFmtDate(DateAdd(strH,strT,Now()), "%a, %d %b %Y %H:%N:%S "&strZ&"")

End Function

And then the function script that was published back in 2001 when ASP Classic was still HOT. The 4guysfromrolla.com posted it and I can imagine it has helped many Time Date format enthusiasts.

Here's the link and blow that is the code just in case 2001 is deleted from the internet. Customizable Date Formatting Routine by Ken Schaefer

    Function fncFmtDate( _
        byVal strDate, _
        byRef strFormat _
       )
     ' Accepts strDate as a valid date/time,
     ' strFormat as the output template.
     ' The function finds each item in the
     ' template and replaces it with the
     ' relevant information extracted from strDate

     ' Template items (example)
     ' %m Month as a decimal (02)
     ' %B Full month name (February)
     ' %b Abbreviated month name (Feb )
     ' %d Day of the month (23)
     ' %O Ordinal of day of month (eg st or rd or nd)
     ' %j Day of the year (54)
     ' %Y Year with century (1998)
     ' %y Year without century (98)
     ' %w Weekday as integer (0 is Sunday)
     ' %a Abbreviated day name (Fri)
     ' %A Weekday Name (Friday)
     ' %H Hour in 24 hour format (24)
     ' %h Hour in 12 hour format (12)
     ' %N Minute as an integer (01)
     ' %n Minute as optional if minute <> 0
     ' %S Second as an integer (55)
     ' %P AM/PM Indicator (PM)
'#### READ THE FORMATTING GUIDE ABOVE #####

'Snipped the code due to the 1% possibility you didn't format your date / time correctly and the code tosses an error based on that formatting.

    End Function ' fncFmtDate

As you can see we have many possible solutions. Find the date time formatting solution that fits your projects and built on top of it.

Ken's code did require a specific input date format. For those that are not really into debugging or creating their own code you can use what I have built from Ken's example above.

This takes just any format in ASP Classic and works it's magic. Notice the first part of the code corrects single digit issues. If you have ever worked with currency formatting you should know about this.

Murray's Active DateFormat to Anything Code reworking what Ken started. Here's the magic part.

 if (DatePart("m", strDate) < 10) then
    twoDigMonth = "0" & DatePart("m", strDate)
 else
    twoDigMonth = DatePart("m", strDate)
 end if

 if (DatePart("d", strDate) < 10) then
    twoDigDay = "0" & DatePart("d", strDate)
 else
    twoDigDay = DatePart("d", strDate)
 end if

Then here's the rest of the code. Take out all the "On Error Resume Next" because they are not needed as long as your input is corrected.

 ' Insert Month Numbers
 strFormat = Replace(strFormat, "%m", _
          DatePart("m", strDate), 1, -1, vbBinaryCompare)

 ' Insert Month Numbers
 strFormat = Replace(strFormat, "%M", _
          twoDigMonth, 1, -1, vbBinaryCompare)

 ' Insert non-Abbreviated Month Names
 strFormat = Replace(strFormat, "%B", _
          MonthName(DatePart("m", strDate), _
          False), 1, -1, vbBinaryCompare)

 ' Insert Abbreviated Month Names
 strFormat = Replace(strFormat, "%b", _
          MonthName(DatePart("m", strDate), _
          True), 1, -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%d", _
          DatePart("d",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Day Of Month
 strFormat = Replace(strFormat, "%D", _
          twoDigDay, 1, _
          -1, vbBinaryCompare)

 ' Insert Day of Month Ordinal (eg st, th, or rd)
 strFormat = Replace(strFormat, "%O", _
          fncGetDayOrdinal(Day(strDate)), _
          1, -1, vbBinaryCompare)

 ' Insert Day of Year
 strFormat = Replace(strFormat, "%j", _
          DatePart("y",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Long Year (4 digit)
 strFormat = Replace(strFormat, "%Y", _
          DatePart("yyyy",strDate), 1, _
          -1, vbBinaryCompare)

 ' Insert Short Year (2 digit)
 strFormat = Replace(strFormat, "%y", _
          Right(DatePart("yyyy",strDate),2), _
          1, -1, vbBinaryCompare)

 ' Insert Weekday as Integer (eg 0 = Sunday)
 strFormat = Replace(strFormat, "%w", _
          DatePart("w",strDate,1), 1, _
          -1, vbBinaryCompare)

 ' Insert Abbreviated Weekday Name (eg Sun)
 strFormat = Replace(strFormat, "%a", _
          WeekDayName(DatePart("w",strDate,1),True), 1, _
          -1, vbBinaryCompare)

 ' Insert non-Abbreviated Weekday Name
 strFormat = Replace(strFormat, "%A", _
          WeekDayName(DatePart("w",strDate,1),False), 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 24hr format
 str24HourPart = DatePart("h",strDate)
 If Len(str24HourPart) < 2 then str24HourPart = "0" & _
                                                 str24HourPart
 strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Hour in 12hr format
 int12HourPart = DatePart("h",strDate) Mod 12
 If int12HourPart = 0 then int12HourPart = 12
 strFormat = Replace(strFormat, "%h", int12HourPart, 1, _
          -1, vbBinaryCompare)

 ' Insert Minutes
 strMinutePart = DatePart("n",strDate)
 If Len(strMinutePart) < 2 then _
          strMinutePart = "0" & strMinutePart
 strFormat = Replace(strFormat, "%N", strMinutePart, _
          1, -1, vbBinaryCompare)

 ' Insert Optional Minutes
 If CInt(strMinutePart) = 0 then
  strFormat = Replace(strFormat, "%n", "", 1, _
           -1, vbBinaryCompare)
 Else
  If CInt(strMinutePart) < 10 then _
           strMinutePart = "0" & strMinutePart
  strMinutePart = ":" & strMinutePart
  strFormat = Replace(strFormat, "%n", strMinutePart, _
           1, -1, vbBinaryCompare)
 End if

 ' Insert Seconds
 strSecondPart = DatePart("s",strDate)
 If Len(strSecondPart) < 2 then _
          strSecondPart = "0" & strSecondPart
 strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
          -1, vbBinaryCompare)

 ' Insert AM/PM indicator
 If DatePart("h",strDate) >= 12 then
   strAMPM = "PM"
 Else
   strAMPM = "AM"
 End If

 strFormat = Replace(strFormat, "%P", strAMPM, 1, _
          -1, vbBinaryCompare)


 fncFmtDate = strFormat

Bundle this all into a nice function call.

Function fncFmtDate(strDate, strFormat)
'Example for your perfect GMT time in Sitemaps, RSSFeeds, Google Stuff, Cookies
'# I'm in Louisiana so I'm -6 hours from GMT 
'# fncFmtDate(DateAdd("h", -6,Now()), "%a, %d %b %H:%N:%S GMT")&"

'# Place code above here.... Don't forget the Magic part.

End Function

There you have it, 100% working live code. If you see an error it's your input not the function. Be sure to follow the formatting example of the GMT I listed.

We can't give perfect solutions for every ASP Classic programmer, we give what has worked for us and basic code practices.
If I thought this was all about Copy and Paste solutions I would have been posting since the 80's. Give it time, work with error traps and learn to read from left to right and top to bottom.

Solution to format placed here: Issue: It outputs : Server current UTC time is: Fri, 15 Jan 2016 07:42:13 UTC But I need in this format: YYYYMMDDHHMMSS

Here's how you use the function to get your format. See how I changed the %a to %Y ? The intructions are in the code.

fncFmtDate(DateAdd("h", -6,Now()), "%Y, %m %d %H:%N:%S GMT")

If you do not need spaces or the comma take it out. But how you show your desired format is something I have not seen in ASP development.

fncFmtDate(DateAdd("h", -6,Now()), ""%Y%m%d%H%N%S GMT")

Work with the code, tell it what you want as your output, read the code.

6
  • Hi its giving error. Isn't there any easy solution?
    – Blue Rose
    Jan 15, 2016 at 4:43
  • @BlueRose I there's the problem right there...I don't want to know stuff just give me a solution!!
    – user692942
    Jan 15, 2016 at 9:42
  • @BlueRose post the error and post how your input data looks. And no, ASP Classic offers no easy solutions. But, once you learn your style it will offer more creative avenues then most experience with any code.
    – Murray W
    Jan 15, 2016 at 14:21
  • @BlueRose You need to be willing to learn. Learn our host language and we will show you the dialects that make it great. We don't do peoples homework or class room assignments and we truly don't program for the profit of others. Show that you are willing to learn, get this issue resolved and move to the next function call.
    – Murray W
    Jan 15, 2016 at 15:03
  • I have posted my latest attempt. I just want timestamp. I have tried many ways. I am completely new to classic asp. plz help @MurrayW
    – Blue Rose
    Jan 18, 2016 at 4:53
0

It depends on what the Date() contains, to begin with. First, you need to know what Time Zone the server is in as Classic ASP takes the date from the Regional System Settings of the Web Server it is running on.

Once you have this just use DateAdd() to offset the hours + or - from the current time zone.

<%
Dim offset
'i.e PST to GMT
offset = -8
Response.Write DateAdd("h", offset, Date())
%>

That is still just raw date though and will output as a string in the default format (based on Regional System Settings from the Server).

For help formatting dates please see Format current date and time

Based on this comment you can use the example in the linked answer to format like this YYYYMMDDHHMMSS by rearranging the date components;

Dim dt
dt = yy & mm & dd & hh & nn & ss
Response.Write dt

The value of the date components is worked out using Now() but you can replace this with

dtsnow = DateAdd("h", offset, Date())

Against my better judgement here is how to do it using the example from the linked answer, just wanted you to attempt it yourself.

Dim dd, mm, yy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue

'UTC and GMT are the same no need for offset.
'Store DateTimeStamp once.
dtsnow = Now() 'Use Now() otherwise we don't have the Time.

'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)

'Build the date string in the format yyyymmdd
datevalue = yy & mm & dd
'Build the time string in the format hhmmss
timevalue = hh & nn & ss
'Concatenate both together to build the timestamp yyyymmddhhmmss
dtsvalue = datevalue & timevalue

'Output to screen
Call Response.Write(dtsvalue)
6
  • Really thanks for your answer. But is there way I can show it on this format YYYYMMDDHHMMSS ?
    – Blue Rose
    Jan 13, 2016 at 2:47
  • @BlueRose All you need to do that is shown in that answer I've linked. I'll update the above answer with a quick example, but have a look at the link it should help.
    – user692942
    Jan 13, 2016 at 7:57
  • I have updated question with some research but still not at the point. Would you please help?
    – Blue Rose
    Jan 15, 2016 at 7:47
  • @BlueRose You don't seem willing to even try, there is more then enough information and examples for you to work this out. I won't be contributing anymore to this question. Good luck.
    – user692942
    Jan 15, 2016 at 7:52
  • I am not posting this question for fun. I am really stuck in this prb and working on it since many days. If you don't know plz shut up. I am really tensed @Lankymart
    – Blue Rose
    Jan 15, 2016 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.