Skip to content

Instantly share code, notes, and snippets.

@ThinhPhan
Created October 4, 2019 09:20
Show Gist options
  • Save ThinhPhan/66c99a70eaf29d45b1e09177787ffe7a to your computer and use it in GitHub Desktop.
Save ThinhPhan/66c99a70eaf29d45b1e09177787ffe7a to your computer and use it in GitHub Desktop.
Javascript code snippets - Date Time Format to String

As toISOString() will only return current UTC time , not local time. We have to make a date by using .toString() function to get date in yyyy-MM-dd format like

let now = new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('T')[0];
// 2019-10-04

To get date and time into in yyyy-MM-ddTHH:mm:ss format

let now = new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0];
//2019-10-04T16:19:18

To get date and time into in yyyy-MM-dd HH:mm:ss format

// 
let now = new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0].replace('T',' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment