1

I am trying to change a specific datetime to user/local timezone datetime. But i couldnt able to convert it. I tried all possible solutions availabe for javascript and tried moment js. It still give me invalid date.

My DateTime Object:

{date: "2017-07-14 14:23:30.000000", timezone_type: 3, timezone: "UTC"}

What I am trying is to convert it to the user timezone or local timezone.

Is there any options available in Moment.js to do it?

In my SugarCRM JavaScript, I couldnt use Date.toUTC.

Methods I tried:

Convert UTC date time to local date time using JavaScript

DonOfDen
  • 3,968
  • 11
  • 62
  • 112
  • So you are trying to convert to timezone for example `timeZone: 'America/Los_Angeles'` or `timeZone: 'America/Detroit'` like this? – Vineesh Jul 17 '17 at 11:16
  • Just converting it to `2017-07-14 19:23:30.000000` my local time is 5+ IST – DonOfDen Jul 17 '17 at 11:22
  • Do not put an answer in the question description, instead, if significantly different from all the other answers, write your own as an answer. You may even accept it if you feel the other answers are not close enough. – Emile Bergeron Jul 17 '17 at 13:57
  • 1
    done @EmileBergeron thats sound correct :) – DonOfDen Jul 17 '17 at 13:59

2 Answers2

1
// input data
var myDateTime = {
    date: "2017-07-14 14:23:30.000000",
    timezone_type: 3,
    timezone: "UTC"
};

// Z in string and format in order to force UTC
var utcMoment = moment(myDateTime.date + "Z", "YYYY-MM-DD HH:mm:ss.SZ");
// convert to time zone of sugar user
var myDate = SUGAR.App.date.UTCtoLocalTime(utcMoment);
// format output
var localDateTime = myDate.format("YYYY-MM-DD HH:mm:ss");

Result:

localDateTime
"2017-07-14 16:23:30"
Jay
  • 3,640
  • 12
  • 17
  • @TomPHP do you mean with .000000 or what format? :) What exactly is your expected output? – Jay Jul 17 '17 at 13:29
  • Like YYYY-MM-DD HH:SS insted of getting fullyear() like that. – DonOfDen Jul 17 '17 at 13:30
  • 1
    I found the solution for it `moment(myDate).format('YYYY-MM-DD HH:mm:ss')` – DonOfDen Jul 17 '17 at 13:46
  • 1
    @TomPHP Ah nice, well done! :) I was somewhat disappointed/surprised by stock javascript not having a .format() function, so good to know that moment() helps out with that. – Jay Jul 17 '17 at 14:59
  • You should never parse strings with the built in Date parser, `new Date("2017-07-14 14:23:30.000000")` returns an invalid date in Safari. – RobG Jul 18 '17 at 05:00
  • I think it would make more sense to put the Z at the end: `myDateTime.date + 'Z'` as that's where most would expect it to be (and adjust the parse format accordingly). – RobG Jul 18 '17 at 10:24
  • Sure, why not :) – Jay Jul 18 '17 at 10:28
1

Found the solution with the help of @jay, did a few modifications and made it work for me.

var myDateTime = {
    date: "2017-07-14 14:23:30.000000", 
    timezone_type: 3, 
    timezone: "UTC"
};
var myDate = SUGAR.App.date.UTCtoLocalTime(new Date(myDateTime.date));
// 2017-07-14 12:20:26
var resultDate = moment(myDate).format('YYYY-MM-DD HH:mm:ss');
// Mon, Jul 17, 2017 10:40 AM 
var resultformat = moment(myDate).format('llll');
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
DonOfDen
  • 3,968
  • 11
  • 62
  • 112