Last active
July 4, 2022 06:00
-
-
Save daanta-real/b36677ad2ee56ad8dbeaa993729abe07 to your computer and use it in GitHub Desktop.
Get new Date class by one string (Cross-browsing available)
This file contains 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
function getDateByString(dateStr) { | |
// String 미입력 시 return | |
if(dateStr == undefined || !dateStr || typeof dateStr != "string" || dateStr.length < 10) return; | |
console.log(dateStr); | |
// YMD | |
var year = parseInt(dateStr.substring(0, 4)); | |
var month = parseInt(dateStr.substring(5, 7)) - 1; | |
var day = parseInt(dateStr.substring(8, 10)); | |
console.log(year, month, day); | |
// HMS | |
if(dateStr.length > 10 && dateStr.length >= 20) { | |
var hour = parseInt(dateStr.substring(11, 13)); | |
var min = parseInt(dateStr.substring(14, 16)); | |
var sec = parseInt(dateStr.substring(17, 19)); | |
console.log(hour, min, sec); | |
return new Date(year, month, day, hour, min, sec); | |
} else return new Date(year, month, day); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment