For example if I do for the above date object something like: value.hours()
, I get as output 16
instead of 18
. I believe it returns the hours in the original GMT time, not like in my date object which is GMT+2. I can of course add 2 to the returned result, but it bees cumbersome. Is there any way to get the hours correctly in my case?
For example if I do for the above date object something like: value.hours()
, I get as output 16
instead of 18
. I believe it returns the hours in the original GMT time, not like in my date object which is GMT+2. I can of course add 2 to the returned result, but it bees cumbersome. Is there any way to get the hours correctly in my case?
4 Answers
Reset to default 1I'm not sure as to what you've already tried, but I put the following into JSFiddle and it worked like a charm. I am currently in CST in America and it is 8:30 in the morning here. When I ran the snippet below I got today's date at 1:30 PM which I would assume is accurate in difference.
HTML
<div id="m1"></div>
JavaScript
var a = moment.tz(new Date(), "GMT");
document.getElementById('m1').innerHTML = a.format("YYYY MM DD; HH:mm");
The Moment.js documentation states the following in regards to creating a Moment
object with a native JavaScript Date
object:
You can create a
Moment
with a pre-existing native JavaScriptDate
object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
This clones the
Date
object; further changes to theDate
won't affect theMoment
, and vice-versa.
To find the information quoted above quickly, when you reach the Moment.js documentation, it is located under the Parse section under sub-section Date.
To display local time:
value.local();
value.hours(); // 18
To reverse:
value.utc();
value.hours(); // 16
I think that you can solve it by doing what the docs says. Something like this:
moment().tz("America/Los_Angeles").format();
https://momentjs./timezone/docs/#/using-timezones/
I guess maybe this works
var localDateString = new Date("GMT-DATETIME").toLocaleString();
//Converting GMT date to your local datetime in string
var localHours = new Date(localDateString).getHours();