Skip to content

Instantly share code, notes, and snippets.

@cybertramp
Last active September 11, 2020 08:39
Show Gist options
  • Save cybertramp/87eb4656960930e2bbb65e287a838a76 to your computer and use it in GitHub Desktop.
Save cybertramp/87eb4656960930e2bbb65e287a838a76 to your computer and use it in GitHub Desktop.
[JS] ISO8601 to YYYY-MM-DD hh:mm:ss convert function
/*
paran_son@outlook.com
INPUT FORMAT EXAMPLE: 2013-07-16T19:00:00Z
OUTPUT FORMAT EXAMPLE: 2013-07-76 19:00:00
*/
datetimeConvert(_iso8601_datetime) {
if (_iso8601_datetime == null) {
return "";
}
if (_iso8601_datetime.indexOf("+") != -1){
_iso8601_datetime = _iso8601_datetime.split("+")
_iso8601_datetime = _iso8601_datetime[0]
}
var date = new Date(_iso8601_datetime);
var year = date.getFullYear();
var month = date.getMonth()+1;
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var dt = date.getDate();
if (dt < 10) {
dt = '0' + dt;
}
if (month < 10) {
month = '0' + month;
}
if (hour < 10) {
hour = '0' + hour;
}
if (min < 10) {
min = '0' + min;
}
if (sec < 10){
sec = '0' + sec;
}
return (year+'-' + month + '-'+dt +" "+hour+":"+min+":"+sec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment