This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 11 months ago.
I have a date object as follows
let dateObj = Sat May 01 2021 20:21:00 GMT-0700 (Pacific Daylight Time)
I want to convert above value in this string format.
let stringVal = 2021-05-01T20:21:00.000+0000
can someone let me know how to achieve this. i Have tried toUTCString(), to DateString(), toISOString() and all other methods but i was not able to achieve the result in the format above.
Any guidance is appreciated. is it even possible ?
Not sure if you wanted it converted to UTC, but based on your example it looks like not.
Maybe something like this? Just replace "new Date()" with your date object.
const date = new Date(new Date() - 1000 * 60 * 60 * 7); // subtract 7 hours since .toISOString() is going to add 7 hours
console.log(date.toISOString().replace('Z','+0000'));
* I should add this assumes the client's timezone is Pacific Daylight Time, or a least something with the same offset
Related
This question already has answers here:
Convert UNIX timestamp to date time (javascript)
(4 answers)
Closed 2 years ago.
This timestamp 1604978063 is not correct according to new Date(1604978063)
It returns the following:
Date Mon Jan 19 1970 05:49:38 GMT-0800 (Pacific Standard Time)
When I look it up on DuckDuckGo, it looks correct.
What's happening here?
Javascript's timestamps are not standard unix timestamps. They use milliseconds and therefore require a multiplication by 1000:
new Date(1604978063 * 1000)
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I need to display a date formatted as a UTC date, and I should do this with Datejs. Is this possible?
I need to do something like
var d = new Date();
var dateString = d.toString("d \n MMM/yyyy \n H:mm:ss");
...but with date and time formatted as UTC.
I looked at the docs but the closest thing I found is
d.toISOString();
which gives me a date as a UTC string but not in the format I want.
edit: changed toUTCSTring to toISOString
Consider using Moment.js, a more well-maintained library for working with dates and times.
moment.utc(d).format("D [\n] MMM/YYYY [\n] H:mm:ss");
If this is not an option, you can shift the date by the timezone offset (which gives you the date/time portion of the UTC date but in the local timezone), then format it:
var d2 = new Date(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
console.log(d2.toString("d \n MMM/yyyy \n H:mm:ss"));
Edit: Removed dependency on built-in date parser.
This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Closed 4 years ago.
I have been trying to calculate the exact time since a very specific date in history using Javascript.
The Date is Feb 24th 2008 17:30 GMT+0
I need help in calculating exact time passed down to the second using Javascript.
Here is the previous date and the current date.
I need help in calculating Hours, Minutes and Seconds since that date/time.
var previousDate = new Date("Sun Feb 24 2008 17:30:00 GMT+0");
var currentDate = new Date();
It's easy to calculate the milliseconds between two dates:
var millis = currentDate - previousDate;
From there you can calculate the seconds:
var seconds = Math.round(millis / 1000);
Calculation of minutes, hours, ... is straightforward (division by 60 or 60*60).
Parsing of date strings in javascript fraught. If you have a specific date, far better to avoid the built–in parser. If it's UTC, use Date.UTC to generate the time value.
Then just subtract from any other date to get the difference in milliseconds and convert to seconds, as hgoebi suggests.
var epoch = new Date(Date.UTC(2008,1,24,17,30));
console.log(epoch.toISOString());
console.log(`Seconds from epoch to now: ${(Date.now() - epoch)/1000|0}`);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
From the server I get '2019-01-19T19:11:00.000Z' I need to convert to local timezone, so that I end up with '2019-01-19T11:11:00'. My UTC offset is 8 hrs.
new Date('2019-01-19T19:11:00.000Z') produces Sat Jan 19 2019 11:11:00 GMT-0800 (Pacific Standard Time), how do I get it back to '2019-01-19T11:11:00'? Thanks
You want the date string in iso format, respecting the local time zone:
const tzoffset = (new Date()).getTimezoneOffset() * 60000;
const d = new Date('2019-01-19T19:11:00.000Z')
console.log(new Date(d - tzoffset).toISOString().split('.')[0])
console.log('2019-01-19T11:11:00')
var now = new Date();
console.log(now.toISOString().split('.')[0]);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
Just wondering how I would format a javascript date to be like
yearmmddThhmm
20130511T0825
The best thing I've found for formatting dates in JS (aside from painstakingly putting the pieces together) is using momentjs. It is lightweight and the API is super simple. Just give it a date object, pass it a few parameters, and bam, get your nicely formatted date back.
Date has toISOString, which is almost there, then just strip out what you don't want.
var d = new Date(), // Tue Jun 04 2013 21:23:52 GMT+0100 (GMT Daylight Time)
s = d.toISOString(); // "2013-06-04T20:23:52.058Z"
s.replace(/[^\da-z]/ig, '').slice(0, -6); // 20130604T2023"