Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
format javascript date to format "YYYY-mm-dd HH:MM:SS"
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
//2015-03-31 13:35:00
@ragunandan

This comment has been minimized.

Copy link

@ragunandan ragunandan commented Jul 5, 2018

Works Perfect!

@rameshsangepu

This comment has been minimized.

Copy link

@rameshsangepu rameshsangepu commented Sep 5, 2018

works like charm!!. Thanks

@glenwell

This comment has been minimized.

Copy link

@glenwell glenwell commented Oct 18, 2018

For UTC...

I tried yours and it was giving a fixed time for some odd reason.

function NOW() {

    var date = new Date();
    var aaaa = date.getUTCFullYear();
    var gg = date.getUTCDate();
    var mm = (date.getUTCMonth() + 1);

    if (gg < 10)
        gg = "0" + gg;

    if (mm < 10)
        mm = "0" + mm;

    var cur_day = aaaa + "-" + mm + "-" + gg;

    var hours = date.getUTCHours()
    var minutes = date.getUTCMinutes()
    var seconds = date.getUTCSeconds();

    if (hours < 10)
        hours = "0" + hours;

    if (minutes < 10)
        minutes = "0" + minutes;

    if (seconds < 10)
        seconds = "0" + seconds;

    return cur_day + " " + hours + ":" + minutes + ":" + seconds;

}

console.log(NOW());

Credits:
dotmaui.com
naryad

@rcebrian

This comment has been minimized.

Copy link

@rcebrian rcebrian commented Oct 27, 2019

When formatting the time you are rounding every 5 minutes.
For example, 23:59 becomes 23:55.

@MythRen

This comment has been minimized.

Copy link
Owner Author

@MythRen MythRen commented Nov 6, 2019

@rcebrian yes, i'm rounding the time every 5 minutes because some reason.

@MythRen

This comment has been minimized.

Copy link
Owner Author

@MythRen MythRen commented Nov 6, 2019

@glenwell because i'm rounding time every 5mins, so you will see fixed time when you execute the code in a small duration (say 11:01 and 11:04, you see only 11:00)

@mohokh67

This comment has been minimized.

Copy link

@mohokh67 mohokh67 commented Jun 1, 2020

Why not to use this one

const formatedTimestamp = ()=> {
  const d = new Date()
  const date = d.toISOString().split('T')[0];
  const time = d.toTimeString().split(' ')[0];
  return `${date} ${time}`
}

https://gist.github.com/mohokh67/e0c5035816f5a88d6133b085361ad15b

@gbrunow

This comment has been minimized.

Copy link

@gbrunow gbrunow commented Jun 11, 2020

@mohokh67 because of this:
image
You could, of course, correct the offset and then use that method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.