Link to home
Start Free TrialLog in
Avatar of rossmcm
rossmcmFlag for New Zealand

asked on

function to return a date in UTC

I want a server side JScript function to convert a date to UTC.  There seem to be a myriad of methods to do with UTC conversion but I can't quite work out how to do it.  I don't want the functionality of Date.toUTCString - I want the result of the function to be a date object so I can use my own formatting routines on it.
Avatar of hongjun
hongjun
Flag of Singapore image

try this.
It will return a date object


<%@Language=JScript%>

<%
function toUTC(){
   var d, s;                   //Declare variables.
   d = new Date();             //Create Date object.
   s = new Date(d.toUTCString());       //Convert to UTC string.
   return(s);                  //Return UTC string.
}

%>

<%
Response.Write(toUTC() + "<br />");
Response.Write(toUTC().getYear() + "<br />");
%>

</body>
</html>
I did a getYear() to confirm it is a date object :)
Avatar of rossmcm

ASKER

OK, it runs without errors but it doesn't do what I want - it seems to return the same value as the input.

Specifically, the problem is this - I have records in a MS Access database that have a date field.  The field gets filled in using the local time at the server, however, when I display the time I want to display it in UTC.  - so I have:

function FormattedShortDate (TheDate)

{
var Day = TheDate.getDate () ;
if (Day < 10)
    {
    Day = "&nbsp;" + Day ;
    }

return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
 
function FormattedShortTime (TheTime)

{
var Hours = TheTime.getHours () ;
if (Hours < 10)
    {
    Hours = "&nbsp;" + Hours ;
    }
var Minutes = TheTime.getMinutes () ;
if (Minutes < 10)
    {
    Minutes = "0" + Minutes ;
    }

return (Hours + ":" + Minutes) ;
}
 

function FormattedShortDateTime (TheDate)

{
return (FormattedShortDate (TheDate) + " " + FormattedShortTime (TheDate)) ;
}
 

function toUTC (ADate)

{
var  
    s   ;

s = new Date (ADate.toUTCString()) ;       //Convert to UTC string.
return (s);
}                

function FormattedShortUTCDateTime (TheDate)

{
return (FormattedShortDateTime (toUTC (TheDate))) ;
}
 
...however the function FormattedShortUTCDateTime seems to return the same string as the function FormattedShortDateTime
Do yo still need help?
If yes, then we can sort it out.
Avatar of rossmcm

ASKER

I gave up, but yes, the problem is still there
I will look into it tonight :)
I am in GMT +8 timezone.
Perhaps it would be good if you can show how you call your function.
Avatar of rossmcm

ASKER

The following is the file at www.acqura.com/datetest.asp.  It produces the same output for both lines.

***************************************************************************
<%@ LANGUAGE = JScript %>

<%
function FormattedShortDate (TheDate)

{
var Day = TheDate.getDate () ;
if (Day < 10)
    {
    Day = "&nbsp;" + Day ;
    }

return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
 
function FormattedShortTime (TheTime)

{
var Hours = TheTime.getHours () ;
if (Hours < 10)
    {
    Hours = "&nbsp;" + Hours ;
    }
var Minutes = TheTime.getMinutes () ;
if (Minutes < 10)
    {
    Minutes = "0" + Minutes ;
    }

return (Hours + ":" + Minutes) ;
}
 

function FormattedShortDateTime (TheDate)

{
return (FormattedShortDate (TheDate) + " " + FormattedShortTime (TheDate)) ;
}
 

function toUTC (ADate)

{
var  
    s   ;

s = new Date (ADate.toUTCString()) ;       //Convert to UTC string.
return (s);
}                

function FormattedShortLocalDateTime (TheDate)

{
return (FormattedShortDateTime (TheDate)) ;
}

function FormattedShortUTCDateTime (TheDate)

{
return (FormattedShortDateTime (toUTC (TheDate))) ;
}

%>

Local date is <%=FormattedShortLocalDateTime (new Date())%><br>
UTC date is <%=FormattedShortUTCDateTime (new Date())%><br>
***************************************************************************
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rossmcm

ASKER

well done.  Where was I going wrong?
The problem is when you do a new Date(string), it will give you the current datetime.