How to convert a number to a date in JavaScript [duplicate] - javascript

This question already has answers here:
Convert UTC Epoch to local date
(16 answers)
Closed 4 years ago.
I have a number, "1546108200", in epoch format.
I want to convert into a date format, like "Sunday, December 30, 2018 12:00:00 AM".
How can I do that?

You can use this:
var date = new Date(1546108200 * 1000);
console.log(date.toUTCString())

Related

How to get a week ago as UTC timestamp in JavaScript? [duplicate]

This question already has answers here:
how do I subtract one week from this date in jquery?
(4 answers)
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 6 months ago.
How can I get a UTC timestamp of a week ago in ISO 8601 format (i.e., YYYY-MM-DDTHH:MM:SSZ) using JavaScript?
I don't want to use any libraries.
Using standard Date functions:
const d = new Date();
d.setDate(d.getDate() - 7);
console.log(d.toISOString());

i want to convert date Data to date? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Converting dates in JavaScript
(5 answers)
Closed 2 years ago.
I get data from open weather api and when i convert it i got the full date i want to extract only the hours :
sunrise: 1607412091
i try this method
var daterise= data.sys.sunrise;
var exactdate= new Date(daterise*1000);
document.getElementById("rise").innerHTML="Sunrise:"+ exactdate;
and i got the full date
Tue Dec 08 2020 08:21:31 GMT+0100 (UTC+01:00)
I want just the hour
i want to extract the hours from the date ??
cosnt hours = exactdate.getHours()

javascript convert Date to this format '2019-01-19T19:11:00' [duplicate]

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]);

Javascript Change timestap format [duplicate]

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

How can I convert milliseconds to date and time string? [duplicate]

This question already has answers here:
How to convert milliseconds into a readable date?
(7 answers)
Closed 9 years ago.
can I convert milliseconds to date and time string. suppose I have millisecond in a long number and I want it like this:
Oct 22 2013 09:50:17
is this possible??
Like this:
var date = new Date(1324339212260);
date.toString("MMM dd");

Categories