Javascript Date.Now() in local time

54519
5
Jump to solution
11-30-2015 04:02 PM
MeleKoneya
Occasional Contributor III

I am writing the current date and time to an attribute in an SDE feature class that is being accessed via a FeatureServer when an edit occurs  with the following:

feature.attributes.sfd_edit_date = Date.now();

It works great, except the time is in UTC and not local when I view it in ArcMap.

I understand that Date.now() gets me the current date in milliseconds,  but how can I get the time to be the current local time and not the UTC time?

Thanks.

Mele

0 Kudos
1 Solution
5 Replies
SteveCole
Frequent Contributor

There's lots of solutions online. Here's one discussion and another.

MeleKoneya
Occasional Contributor III

Thanks Steve the quick reply.   I was not able to open the links you sent.

0 Kudos
MeleKoneya
Occasional Contributor III

This is the code I pulled together from StackOverFlow links.    It writes the date to the SDE feature class in the correct format to view local time in ArcMap.

feature.attributes.sfd_edit_date = convertUTCDateToLocalDate(new Date(Date.now()));

//added to convert UTC to local Time
    function convertUTCDateToLocalDate(date) {
    var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);

    var offset = date.getTimezoneOffset() / 60;
    var hours = date.getHours();

    newDate.setHours(hours - offset);
    newDate = newDate.getTime();
    return newDate;  
    };

MeleKoneya
Occasional Contributor III

Changed my code slightly as shown below:

//added to convert UTC to local Time

    function convertUTCDateToLocalDate(date) {

    date = new Date(date);

    var localOffset = date.getTimezoneOffset() * 60000;

    var localTime = date.getTime();

    date = localTime - localOffset;

    //date = new Date(date);

    return date;

    };

0 Kudos