Chadrick Blog

get timestamp in javascript

unfortunately there isn’t any native way to get formatted timestamp. Here is a script that does the job.

var d = new Date()

let \[h,m,s\] = d.toLocaleTimeString().split(':')

let \[year,month,day\] = d.toLocaleDateString().split('-')

if(year.length >2){
    year = year.slice(2)
}

if(month.length<2){
    month = '0'+month
}

if(day.length <2){
    day = '0' + day
}

timestamp = year+month+day+'\_'+ h+m+s

this will output YYMMDD_HHMMSS format timestamp