This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Convert UNIX timestamp to date time (javascript)
(4 answers)
Closed 3 years ago.
I convert a date to a number by running
Date.parse(doc.createdAt);
sO i GET THIS:
1576699528000
which probably turns it into a string as its getting passed from the server to the front end and then back to the server
How do I turn it back into a date object?
Just use constructor of Date:
let mls = Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');
let date = new Date(mls );
An example
let mls = Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');
let date = new Date(mls);
console.log(date);
Try using Date.prototype.setTime() if you have a date object to shift over or just pass it to the constructor, ex. new Date(num)
Related
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Google Apps Script date format issue (Utilities.formatDate)
(2 answers)
Closed 2 years ago.
Can anyone tell me why this code would produce two different dates?
let now = new Date(); // today's date (1/2/2021)
Logger.log(now); // Sat Jan 02 09:42:47 GMT-08:00 2021
Logger.log(new Date(now.getTime()-(2*1000*60*60*24))); // Thu Dec 31 09:42:47 GMT-08:00 2020
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY")); // 12/31/2021
Why would Utilities.formateDate() change the date from 12/31/2020 to 12/31/2021?
******** SOLUTION *********
Change the date format from "MM/d/YYYY" to "MM/d/yyyy".
It's just a simple formatting problem. Look here
This:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY"));
Should be this:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/yyyy"));
In other words just make the capital Y's lower case y's and you're there.
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:
Why does Date.parse give incorrect results?
(11 answers)
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 5 years ago.
I'm getting crazy with showing a date correctly to the client:
From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z
The client to show that data looks like this:
var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);
The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.
Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).
How can I get rid of it?
You're not in the same time zone.
2017-09-06T23:59:59Z is UTC
which is same as
Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Convert PHP Unix Timestamp to Javascript Timestamp format
(1 answer)
Closed 6 years ago.
I initialize my timestamp variable like
var current_time = new Date();
My output is in
Tue Oct 11 2016 15:09:04 GMT+0800 (Malay Peninsula Standard Time)
format. How can I change it to 2016-10-11 07:19:48pm format?
you can acheive this using moment.js lib,
just run
moment().format('YYYY-MM-DD hh:mm:SS A'); // print 2016-10-11 03:20:88 PM
This question already has answers here:
Why isn't "2016-02-16" equal to "2016-02-16 00:00"?
(5 answers)
Closed 6 years ago.
I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:
var dateString = "2016-04-10";
var date = new Date(dateString);
// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)
What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?
Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.
If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.