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
Solved! Go to Solution.
There's lots of solutions online. Here's one discussion and another.
Thanks Steve the quick reply. I was not able to open the links you sent.
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;
};
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;
};