Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert javascript date format to YYYY-MM-DDTHH:MM:SS

how to convert default date in javascript to YYYY-MM-DDTHH:MM:SS format.

new Date() returns Sun Aug 07 2016 16:38:34 GMT+0000 (UTC)

and i need the current date to above format.

like image 245
Vik Avatar asked Aug 07 '16 16:08

Vik


4 Answers

As new Date().toISOString() will return current UTC time, to get local time in ISO String format we have to get time from new Date() function like the following method

document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]);
like image 131
jafarbtech Avatar answered Nov 18 '22 23:11

jafarbtech


You can use moment.js library to achieve this.

Try:

var moment = require('moment')
let dateNow = moment().format('YYYY-MM-DDTHH:MM:SS')
like image 40
azmat fayaz Avatar answered Nov 18 '22 23:11

azmat fayaz


This could work :

var date = new Date();
console.log(date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
like image 5
kevin ternet Avatar answered Nov 18 '22 23:11

kevin ternet


For use the date in ISO 8601 format run the command lines below on NodeJS console or on browser console:

$ node
> var today = new Date(); // or
> var today = new Date(2016, 8, 7, 14, 06, 30); // or
> var today = new Date('2016-08-07T14:06:30');
> today; // Show the date in standard USA format. Grrrr!
> today.toISOString();

For more information, consult the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

like image 2
Fernando Santucci Avatar answered Nov 18 '22 22:11

Fernando Santucci