Skip to content

Instantly share code, notes, and snippets.

@kou10mo
Created November 27, 2019 07:23
Show Gist options
  • Select an option

  • Save kou10mo/8adf5e96a6d8f25a4d9e4328da632643 to your computer and use it in GitHub Desktop.

Select an option

Save kou10mo/8adf5e96a6d8f25a4d9e4328da632643 to your computer and use it in GitHub Desktop.
Mysql等の日時からISO-8601形式に変換
/**
* https://qiita.com/unbabel/items/12487e85525ba1ec1618の改良版
*/
function replaceDate(dateStr) {
const regexp = /^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])(?: ([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
return dateStr.replace(regexp, (match, year, month, day, hour, minutes, seconds) => {
console.log(hour);
if (!hour) {
return `${year}-${month}-${ day}T00:00:00.000+09:00`;
} else {
return `${year}-${month}-${ day}T${hour}:${minutes}:${seconds}.000+09:00`;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment