moment to date object timezone issue - angularjs

I have a moment.js object generated from fullcalendar in BST that looks like this:
console.log(momentSelected)
//Moment {_isAMomentObject: true, _isUTC: true, _offset: 0, _locale: f, _d: Tue May 03 2016 01:00:00 GMT+0100 (BST)…}
I don't want a BST time but a UTC time that looks like this:
console.log(momentSelected.format('YYYY-MM-DD HH:mm Z'));
//2016-05-03 00:00 +00:00
Now I need to convert it into a Date object:
$scope.date = new Date(momentSelected.format('YYYY-MM-DD HH:mm Z'));
console.log($scope.date);
//Wed May 04 2016 01:00:00 GMT+0100 (BST)
The last output is wrong... I want Wed May 04 2016 00:00:00+00:00 (UTC)

You can create a Date object using the toDate function on the moment object.
$scope.date = momentSelected.toDate();
However, you must recognize that the nature of the Date object is that it will always represent UTC internally, and its toString function will always reflect the local time zone where the code is running.
If your local time zone is UK (alternating between GMT and BST for daylight saving time), then it is impossible to get (UTC) time in the string produced by console.log($scope.date);, regardless of how you created that date.
This is why it is better to use moment's format function and display that string directly. A moment can reflect UTC, local time, and other time zones. A Date object cannot.
Also, you should pay no attention to the underscore-prefixed internal fields of a moment object. Use the public API instead. See the moment user guide.

Related

How to remove a specific field from specific document in MongoDB?

db.collection.insertMany({_id:123, name:"abc", loc:"Delhi", YOB:new Date("1991-08-17")}, {_id:124, name:"def", loc:"Bengaluru", YOB:Date("1996-03-08")})
I want to change date format from new Date to Date in first document. How to change a specific field from a specific document like this?
I tried to change the date value from New Date format i.e., ISODate("1991-08-17T00:00:00:000Z") to Date format i.e., Mon Aug 17 2022 03:19:39 GMT+0530 (India Standard Time)
Can anyone help me out?

how to convert UTC date/time into MDT and military time for react application?

I am trying to convert UTC into MDT (mountain time) format. I am not sure which moment format require to convert date time into MDT. i tried moment.fromOADate(41493) but not working. TIA!
Mountain time is GMT -6, so you should be able to use moment().utcOffset(-6)
https://en.wikipedia.org/wiki/Mountain_Time_Zone
https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/
If you're worried about accounting for daylight savings time and can't just subtract 6 hours, you might also look into the moment.js timezone library.
You could say something like is described here and do var zone = moment.tz.zone('America/Chihuahua') (this is mountain time), then get your time offset with zone.parse(utcTimestamp)

Save Date on SQL Server with wrong timezone

I have this:
Clock = Date.now();
this.newReport.DateTime = new Date(this.Clock);
Client side the date is ok: Mon May 20 2019 19:08:34 GMT+0200
But on SQL Server it is save with time 17:08
Why?
Thanks
It looks like that's the same date, when you include the Timezone offset. If the "client side" date is GMT +0200, then the datetime stored should be 17:08:34 (as GMT time).
You're not including much detail, but I expect that you are wanting to save the time as local time. You could either convert the time to Localtime before saving it (and lose the additional information about the timezone), or save the GMT offset along with the date and time so that you could have both available if you wanted to convert it back to localtime later.

Time conversion to IST

I'm trying to convert date & time stamp to IST using Moment. I tried following
moment(1369266934311).utcOffset("+05:30").format() I'm getting output as 2017-12-05T00:00:00+05:30.
But my required format is Tue Dec 05 2017 00:00:00 GMT+0530 (India Standard Time).
Get the date object from the moment object and call its .toString() method.
moment(1369266934311).utcOffset("+05:30").toDate().toString();
// => "Thu May 23 2013 05:25:34 GMT+0530 (IST)"
And, by the way, 1369266934311 is a different date from what you mentioned in your question.

Timezone offset not working in angular js date expression

I have 2016-10-21T13:47:02.922452 as ISO string from backend server.
My timezone is +0530 GMT i.e Offset is +530 (ahead of GMT).
When i use angular date expression like this
{{'2016-10-21T13:47:02.922452'| date:'medium':'+530'}}
I expected output to be = Oct 21, 2016 7:17:02 PM
but it prints
Oct 21, 2016 1:47:02 PM instead.
I am confused for what am i doing wrong here.
Do something like this
var d = new Date('2016-10-21T13:47:02.922452');
console.log(d)
Answering myself !
The easiest way that i figured out ! Make a custom filter
app.filter('IST', function($filter){
return function(val){
var date = new Date(val);
return $filter(date, 'medium');
}
})
Then use filter in expression like this -
{{'2016-10-21T13:47:02.922452'| IST}}
Custom filter will automatically convert ISO format string to Date object (browser timezone will automatically take care of timezone conversion.)
Date pipe transforms the date string you are passing ('2016-10-21T13:47:02.922452') to a Date object and then applies the time zone offset.
¿Whats the problem? When transforming '2016-10-21T13:47:02.922452' to a Date, it supposes that the date is in your local time, so the final calculation is wrong. For example, I am in +0200 so the transformation will be:
2016-10-21T13:47:02.922452 -> 2016-10-21T13:47:02.922452+0200 (Date object)
2016-10-21T13:47:02.922452+0200 -> 2016-10-21T18:17:02.922452+0530 (date changes with offset difference 0530-0200)
If your backend date is always in GMT timezone, just add Z to your date string: 2016-10-21T13:47:02.922452Z. This way, when creating the Date it will understand that the initial timezone is +0000
Solution:
{{'2016-10-21T13:47:02.922452Z' | date:'medium':'+0530'}}

Resources