Reputation: 9289
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.
Upvotes: 9
Views: 44633
Reputation: 11
use this code:
new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]);
Upvotes: 0
Reputation: 1
let date = new Date(Date.now());
date = now.toISOString().substr(0,now.toISOString().indexOf("."));
//2021-07-26T09:07:59
Upvotes: 0
Reputation: 11
moment is to large, you can use fecha
import fecha from 'fecha'
const uTime ="2019-10-21T18:57:33"
fecha.format(fecha.parse(uTime, "YYYY-MM-DD'T'HH:mm:ss"), 'YYYY.MM.DD HH:mm:ss')
// console.log(2019.10.21 18:57:33)
Upvotes: 1
Reputation: 7015
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]);
Upvotes: 13
Reputation: 101
You can use moment.js library to achieve this.
Try:
var moment = require('moment')
let dateNow = moment().format('YYYY-MM-DDTHH:MM:SS')
Upvotes: 10
Reputation: 470
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
Upvotes: 2
Reputation: 4612
This could work :
var date = new Date();
console.log(date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
Upvotes: 5