Skip to content

Instantly share code, notes, and snippets.

@Ivlyth
Last active March 28, 2024 10:54
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ivlyth/c4921735812dd2c0217a to your computer and use it in GitHub Desktop.
Save Ivlyth/c4921735812dd2c0217a to your computer and use it in GitHub Desktop.
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
Copy link

Works Perfect!

@upegnasrame
Copy link

works like charm!!. Thanks

@glenwell
Copy link

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
Copy link

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

@Ivlyth
Copy link
Author

Ivlyth commented Nov 6, 2019

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

@Ivlyth
Copy link
Author

Ivlyth 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
Copy link

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
Copy link

gbrunow commented Jun 11, 2020

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

@xiaoyu2er
Copy link

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

😎

@lorimay21
Copy link

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

Thank you very much!!

@Eden-Harris
Copy link

Eden-Harris commented Sep 26, 2021

😋

const ISO = (timeStamp=Date.now()) => {
	return new Date(timeStamp - (new Date().getTimezoneOffset() * 60 * 1000)).toISOString().slice(0,-5).split('T')
}

console.log('one->',ISO(new Date('2020-09-26 11:28:55').getTime()))
console.log('now->',ISO())
one-> [ '2020-09-26', '11:28:55' ]
now-> [ '2021-09-26', '20:13:36' ]

js 时间格式化 date format YYYY-mm-dd HH:MM:SS

@dexterque
Copy link

这个也行吧?new Date().toLocaleString().replaceAll("/", "-")
问这个问题得大概率都是国内的吧🤣

@imamiao
Copy link

imamiao commented Jul 25, 2022

这个也行吧?new Date().toLocaleString().replaceAll("/", "-") 问这个问题得大概率都是国内的吧🤣

简单好用 中国人不骗中国人🤣

@jonasfrey
Copy link

jonasfrey commented Aug 10, 2022

var f_s_ymd_hms = function(n_unix_ts_ms){
  var o_date = new Date(n_unix_ts_ms);
  var s_hms_ymd = `${o_date.getFullYear().toString().padStart(2,'0')}-${(o_date.getMonth()+1).toString().padStart(2,'0')}-${o_date.getDate().toString().padStart(2,'0')} ${o_date.getHours().toString().padStart(2,'0')}:${o_date.getMinutes().toString().padStart(2,'0')}:${o_date.getSeconds().toString().padStart(2,'0')}`
}

@tarley
Copy link

tarley commented Nov 8, 2023

const now = new Date()
const [date, time] = now.toISOString().substring(0, 19).split('T')
return \`${date} ${time}\`

@jonasfrey
Copy link

import {
    f_b_daylight_saving_time,
    f_s_isotimezone__from_s_timezone,
    f_s_ymd__from_n_ts_ms_utc,
    f_s_dmy__from_n_ts_ms_utc,
    f_s_hms__from_n_ts_ms_utc,
    f_s_ymd_hms__from_n_ts_ms_utc,
    f_n_ms_offset_from_s_timezone_n_ts_ms, 
    f_o_ts_range__day__from_n_ts_ms_utc,
    f_o_ts_range__week__from_n_ts_ms_utc,
    f_o_ts_range__month__from_n_ts_ms_utc,
    f_o_ts_range__year__from_n_ts_ms_utc,
    f_s_timestring_from_n_ms,
    f_measure_time,
    f_n_ts_ms__rounded_to
    }
    from "https://deno.land/x/date_functions@1.3/mod.js"

@daochild
Copy link

Or ...

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment