Skip to content

Instantly share code, notes, and snippets.

@ntuaha
Last active September 20, 2021 21:36
Show Gist options
  • Save ntuaha/f4b16ad377505a8519c7 to your computer and use it in GitHub Desktop.
Save ntuaha/f4b16ad377505a8519c7 to your computer and use it in GitHub Desktop.
Javascript 漂亮日期顯示 YYYYMMDDhhmmss or YYYY-MM-DD hh:mm:ss
'use strict'
function pad(v){
return (v<10)?'0'+v:v
}
function getDateString(d){
var year = d.getFullYear();
var month = pad(d.getMonth()+1);
var day = pad(d.getDate());
var hour = pad(d.getHours());
var min = pad(d.getMinutes());
var sec = pad(d.getSeconds());
return year+month+day+hour+min+sec;
//return year+"-"+month+"-"day+" "+hour+":"+min+":"+sec;
//YYYYMMDDhhmmss
}
const pad = (v) => {
return (v<10)?'0'+v:v
}
const getDateString = (d) => {
let year = d.getFullYear()
let month = pad(d.getMonth()+1)
let day = pad(d.getDate())
let hour = pad(d.getHours())
let min = pad(d.getMinutes())
let sec = pad(d.getSeconds())
//YYYYMMDDhhmmss
return year+month+day+hour+min+sec
//YYYY-MM-DD hh:mm:ss
//return year+"-"+month+"-"day+" "+hour+":"+min+":"+sec
}
@pulipulichen
Copy link

感謝

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