Created
March 7, 2021 11:08
-
-
Save kgsi/3749acc8cf82ddb548a01a9623e248e1 to your computer and use it in GitHub Desktop.
日付フォーマット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 日付をフォーマットする | |
* @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