9

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.

Vik
  • 8,721
  • 27
  • 83
  • 168
  • 2
    You have `dateobj.getFullYear()`, `dateobj.getMonth()`, `dateobj.getDate()`, `dateobj.getHours()`, `dateobj.getMinutes()`, `dateobj.getSeconds()`. Easy, right? – Niet the Dark Absol Aug 07 '16 at 16:45
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – JJJ Aug 07 '16 at 16:46
  • Your format assumes local, how will others know the offset? – RobG Aug 08 '16 at 03:35

7 Answers7

13

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]);
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
  • 1
    This is the best answer as it doesn't require any third party libraries. It sucks that JS doesn't have a cleaner option though. – Ovidio Reyna Mar 14 '19 at 16:24
  • This is the best answer I have found. It's very useful when your API accept that data format. You use it that way: new Date(YourDate.toString().split('GMT')[0] + ' UTC').toISOString().split('.')[0] (second new Date() replace with your variable :) – greygreg87 May 09 '19 at 07:26
9

You can use moment.js library to achieve this.

Try:

var moment = require('moment')
let dateNow = moment().format('YYYY-MM-DDTHH:MM:SS')
Vadim
  • 8,701
  • 4
  • 43
  • 50
azmat fayaz
  • 91
  • 1
  • 3
  • I know this was posted a while back but the format should be 'YYY-MM-DDTHH:mm:ss otherwise you will get month value for minutes and fractional seconds to two digits for actual seconds – Mych Jan 06 '20 at 11:46
5

This could work :

var date = new Date();
console.log(date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
2

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

1

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)
周达理
  • 11
  • 1
  • Welcome to SO! When you reply to a question, try to explain your answer a little bit. In this case, there are some more replies so you should expose Cons and Pros of your answer. – David García Bodego Oct 21 '19 at 11:44
0
let date = new Date(Date.now());
date = now.toISOString().substr(0,now.toISOString().indexOf("."));

//2021-07-26T09:07:59

Chris
  • 1
  • 1
  • 3
    Hi and welcome to stackoverflow, here are some guidelines : **it is not recommended** to post code-only answers, answers should provide more explanation about the code to make it the answer more useful and are more likely to attract upvotes – I_love_vegetables Jul 25 '21 at 04:31
0

use this code:

new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0]);

Christian
  • 4,902
  • 4
  • 24
  • 42
Henry
  • 11
  • 4