Javascript Change timestap format [duplicate] - javascript

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

Related

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()

Convert date to format yyyy-MM-dd+timezone [duplicate]

This question already has answers here:
how to format date in Component of angular 5
(4 answers)
Closed 3 years ago.
I wanna change the input date with time and time zone to this format yyyy-MM-dd+timezone
Example:
I have this input
Sat Oct 05 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
I want an output like this
2019-10-05+2:00
Is there a simple way to convert this?
You can do this with simple Date functions.
public transform(date: Date): string {
return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate() >= 10 ? date.getDate() : '0'+date.getDate()}${date.getTimezoneOffset()}`;
}

javascript datetime in custom format as per timezone [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 4 years ago.
I want datetime in custom format as per below.
2019-01-24T05:52:29:420 +0000 GMT+0000
but when I try do it using below syntax I did not work.
var currentdate = new Date();
alert(currentdate);
Any help? Thanks
Look into Moment.js, you can format JS dates with this quite easily.
e.g:
moment().format('MMMM Do YYYY, h:mm:ss a'); // January 24th 2019, 5:13:29 pm
moment().format('dddd'); // Thursday
in your case:
moment(currentdate).format('the format you would like');
https://momentjs.com/
its very simple you just have to do currentdate.toISOString() which will return "2019-01-24T06:19:19.975Z", now its simple just remove last char from string and concat the other string part in it.
var currentdate = new Date().toISOString();
alert(currentdate.substr(0, currentdate.length-1)+' +0000 GMT+0000');

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

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())

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

Categories