rossmcm
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.
I did a getYear() to confirm it is a date object :)
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 = " " + Day ;
}
return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
function FormattedShortTime (TheTime)
{
var Hours = TheTime.getHours () ;
if (Hours < 10)
{
Hours = " " + 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
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 = " " + Day ;
}
return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
function FormattedShortTime (TheTime)
{
var Hours = TheTime.getHours () ;
if (Hours < 10)
{
Hours = " " + 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.
If yes, then we can sort it out.
ASKER
I gave up, but yes, the problem is still there
I will look into it tonight :)
I am in GMT +8 timezone.
I am in GMT +8 timezone.
Perhaps it would be good if you can show how you call your function.
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 = " " + Day ;
}
return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
function FormattedShortTime (TheTime)
{
var Hours = TheTime.getHours () ;
if (Hours < 10)
{
Hours = " " + 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 FormattedShortLocalDateTim e (TheDate)
{
return (FormattedShortDateTime (TheDate)) ;
}
function FormattedShortUTCDateTime (TheDate)
{
return (FormattedShortDateTime (toUTC (TheDate))) ;
}
%>
Local date is <%=FormattedShortLocalDate Time (new Date())%><br>
UTC date is <%=FormattedShortUTCDateTi me (new Date())%><br>
************************** ********** ********** ********** ********** *********
**************************
<%@ LANGUAGE = JScript %>
<%
function FormattedShortDate (TheDate)
{
var Day = TheDate.getDate () ;
if (Day < 10)
{
Day = " " + Day ;
}
return (Day + "-" + (1 + TheDate.getMonth ()) + "-" + TheDate.getYear ()) ;
}
function FormattedShortTime (TheTime)
{
var Hours = TheTime.getHours () ;
if (Hours < 10)
{
Hours = " " + 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 FormattedShortLocalDateTim
{
return (FormattedShortDateTime (TheDate)) ;
}
function FormattedShortUTCDateTime (TheDate)
{
return (FormattedShortDateTime (toUTC (TheDate))) ;
}
%>
Local date is <%=FormattedShortLocalDate
UTC date is <%=FormattedShortUTCDateTi
**************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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().get
%>
</body>
</html>