Skip to content

Instantly share code, notes, and snippets.

@kgsi
Created March 7, 2021 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgsi/3749acc8cf82ddb548a01a9623e248e1 to your computer and use it in GitHub Desktop.
Save kgsi/3749acc8cf82ddb548a01a9623e248e1 to your computer and use it in GitHub Desktop.
日付フォーマット
/**
* 日付をフォーマットする
* @param {Date} date 日付
* @param {String} [format] フォーマット
* @return {String} フォーマット済み日付
*/
const convertDateFormat = (date, format = 'YYYY-MM-DD hh:mm:ss.SSS') => {
format = format.replace(/YYYY/g, date.getFullYear())
format = format.replace(/MM/g, ('0' + (date.getMonth() + 1)).slice(-2))
format = format.replace(/DD/g, ('0' + date.getDate()).slice(-2))
format = format.replace(/hh/g, ('0' + date.getHours()).slice(-2))
format = format.replace(/mm/g, ('0' + date.getMinutes()).slice(-2))
format = format.replace(/ss/g, ('0' + date.getSeconds()).slice(-2))
if (format.match(/S/g)) {
const milliSeconds = ('00' + date.getMilliseconds()).slice(-3)
const length = format.match(/S/g).length
for (let i = 0; i < length; i++) format = format.replace(/S/, milliSeconds.substring(i, i + 1))
}
return format
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment