I need correct time from `1609891200000` without using Moment - javascript

var now = moment(1609891200000, "x").format('MMM DD h:mm A');
var x = new Date(1609891200000);
console.log(now); // Prints Jan 06 12:00 AM
console.log(x.toLocaleTimeString()); // Prints 05:30:00
I don't know why I keep getting 5:30 as time. I need a way to get the correct time i.e 12:00 AM without the use of Moment.
How can I do this?

You should provide a code of time zone,
If there is no code, then it will provide by default
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
// toLocaleTimeString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleTimeString());
// → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
Instead, you can use
var x = new Date(1609891200000);
x.toGMTString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toUTCString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toISOString() // "2021-01-06T00:00:00.000Z"

Related

Converting epoch time to GMT Date Time Javascript

I am currently using moment js in my application and not able to figure out how to convert epoch time to GMT date time. Providing my code below:
click: (event) => {
console.log(moment.utc(event.point.category).toDate());
}
event.point.category is providing epoch time which I want to convert to GMT date time object, but above code is converting it to local timezone date object.
For ex. 1606262400000 is getting converted to Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) instead of GMT date time which is Wednesday, November 25, 2020 12:00:00 AM
You should get the (UTC) offset (in minutes) of the client to correct time
click: (event) => {
const offset = new Date().getTimezoneOffset();
const targetTime = moment.utc(event.point.category).toDate();
const offsetTime = new Date(targetTime.getTime() + offset * 60 * 1000);
console.log(offsetTime);
}
I believe all that you are missing is a toUTCString().
An epoch timestamp will be the same for Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) and Wednesday, November 25, 2020 12:00:00 AM GMT
click: (event) => {
console.log(moment.utc(event.point.category).toDate().toUTCString());
}
To remain as a Date object, use moment.utc().format()
You can try something like this,
click: (event) => {
console.log(moment(moment.utc(event.point.category).toISOString()).utc());
}

Moment.js - How to convert date string into date?

Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typed
I am using Moment.js to convert a date string into a date field based on user input in the text box.
This is to prevent the problem I described in the linked post for Safari and Firefox not able to render the date when Chrome is fine.
Here is the code snipper:
var tempDate = moment(userInputFieldDate).format('DD-MM-YYYY');
alert(tempDate);
In Chrome, it does work fine (it use to work with the Javascript Date object too) but gives me the moment.js deprecation warning
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
Error
On Firefox and Safari is just gives an UNDEFINED DATE in the alert window. So not entirely sure what should I be doing to convert the date string to a Date object.
Any suggestions on this issue?
If you are getting a JS based date String then first use the new Date(String) constructor and then pass the Date object to the moment method. Like:
var dateString = 'Thu Jul 15 2016 19:31:44 GMT+0200 (CEST)';
var dateObj = new Date(dateString);
var momentObj = moment(dateObj);
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
In case dateString is 15-07-2016, then you should use the moment(date:String, format:String) method
var dateString = '07-15-2016';
var momentObj = moment(dateString, 'MM-DD-YYYY');
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
Sweet and Simple!
moment('2020-12-04T09:52:03.915Z').format('lll');
Dec 4, 2020 4:58 PM
OtherFormats
moment.locale(); // en
moment().format('LT'); // 4:59 PM
moment().format('LTS'); // 4:59:47 PM
moment().format('L'); // 12/08/2020
moment().format('l'); // 12/8/2020
moment().format('LL'); // December 8, 2020
moment().format('ll'); // Dec 8, 2020
moment().format('LLL'); // December 8, 2020 4:59 PM
moment().format('lll'); // Dec 8, 2020 4:59 PM
moment().format('LLLL'); // Tuesday, December 8, 2020 4:59 PM
moment().format('llll'); // Tue, Dec 8, 2020 4:59 PM
if you have a string of date, then you should try this.
const FORMAT = "YYYY ddd MMM DD HH:mm";
const theDate = moment("2019 Tue Apr 09 13:30", FORMAT);
// Tue Apr 09 2019 13:30:00 GMT+0300
const theDate1 = moment("2019 Tue Apr 09 13:30", FORMAT).format('LL')
// April 9, 2019
or try this :
const theDate1 = moment("2019 Tue Apr 09 13:30").format(FORMAT);

How to set date always to eastern time regardless of user's time zone

I have a date given to me by a server in unix time: 1458619200000
NOTE: the other questions you have marked as "duplicate" don't show how to get there from UNIX TIME. I am looking for a specific example in javascript.
However, I find that depending on my timezone I'll have two different results:
d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)
// Now I set my computer to Eastern Time and I get a different result.
d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)
So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?
You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,
var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)
dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)
var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)
Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!
Moment.js (http://momentjs.com/timezone) is your friend.
You want to do something like this:
var d = new Date(1458619200000);
var myTimezone = "America/Toronto";
var myDatetimeFormat= "YYYY-MM-DD hh:mm:ss a z";
var myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);
console.log(myDatetimeString); // gives me "2016-03-22 12:00:00 am EDT"
For daylight saving, Eastern time become 4 hours behind UTC. That's why its offset is -4x60 = -240 minutes. So when daylight is not active the offset will be -300. The offset variable's value is the key point to be noted here. Kindly see this code in action in attached image.
var offset = new Date().getTimezoneOffset();// getting offset to make time in gmt+0 zone (UTC) (for gmt+5 offset comes as -300 minutes)
var date = new Date();
date.setMinutes ( date.getMinutes() + offset);// date now in UTC time
var easternTimeOffset = -240; //for dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300
date.setMinutes ( date.getMinutes() + easternTimeOffset);

Converting UTC to Pacific time using Moment Timezone (javascript)

I am trying to set "formattedLocalTime" to the Pacific time and my 4 lines of code look as below.
Though the chrome debugger displays "locTime" as "Tue Sep 30 2014 16:17:25" which is the correct value I expect, the formattedLocalTime in the last line is "09/30/2014 11:17 pm" which is UTC time and not the timezone I have set (America/Los_Angeles) which should be "09/30/2014 4:17 pm" (4:17 instead of 11:17)
Would highly appreciate any suggestions.
var timestamp = 1412144245453; // Tue Sep 30 2014 23:17:25
var utc = moment.tz(timestamp, "Etc/UTC"); // Tue Sep 30 2014 23:17:25 (displayed in chrome debugger)
var locTime = utc.clone().tz("America/Los_Angeles"); // Tue Sep 30 2014 16:17:25 (displayed in chrome debugger)
var formattedLocalTime = moment(locTime).format("MM/DD/YYYY h:mm a")
You can do this in one step:
moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a')
OUTPUT: "09/30/2014 11:17 pm"
Also, you had evaluated the times for this timestamp incorrectly. In UTC, this timestamp is October 1st, 2014 6:17:25 AM. The corresponding Pacific time is indeed September 30th, 2014, 11:17:25 PM.
You can check this using a site like epochconverter.com, or in moment.js like so:
moment.utc(1412144245453).format() // "2014-10-01T06:17:25+00:00"
try to use:
var formattedLocalTime = locTime.format("MM/DD/YYYY h:mm a")
if you write moment(locTime) then your datetime will be converted back to local time
Use: moment-timezone - TypeError: moment().tz is not a function
const moment = require('moment-timezone');
const time = moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a');
console.log("time : ", time);
Output: time : 09/30/2014 11:17 pm

Javascript UTC timestamp to Local Timezone

I'm trying to convert a timestamp being returned from a JSON resource in javascript that is displaying in UTC to the users local timezone. Below i'm trying to adjust with the user offset.
Example UTC output for date:
Tue Mar 27 2012 02:29:15 GMT-0400 (EDT)
Code
var date = new Date(data.date_created); //Data.date_created coming from json payload
var offset = date.getTimezoneOffset() //Get offset
var new_date = new Date(date offset); //Add offset to userdate
I'm struggling with the appropriate method to achieve this. Can anyone point me in the right direction?
I might be missing something but
var date = new Date( data.date_created );
does what I think you want.
>>> d=new Date('Tue Mar 27 2012 02:29:15 GMT-0800')
Date {Tue Mar 27 2012 06:29:15 GMT-0400 (EDT)}
>>> d.toLocaleString()
"Tue Mar 27 06:29:15 2012"
>>> d=new Date('Tue Mar 27 2012 02:29:15 GMT+0300')
Date {Mon Mar 26 2012 19:29:15 GMT-0400 (EDT)}
>>> d.toLocaleString()
"Mon Mar 26 19:29:15 2012"
Note how changing the GMT offset from -8 to +3 changes the resulting time by 11 hours.

Categories

Resources