Created
June 11, 2016 05:55
-
-
Save Restoration/d6dbdaffbaf4341f9bc8f9364324558d to your computer and use it in GitHub Desktop.
YYYMMDD形式をYYYY/MM/DD形式に変換
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
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <script> | |
| var date = new Date(); | |
| var year = date.getFullYear(); | |
| var month = date.getMonth() +1; | |
| var day = date.getDay(); | |
| function zeroFormat(num){ | |
| if(num < 10){ | |
| return '0' + String(num); | |
| } else { | |
| return num; | |
| } | |
| } | |
| var year = zeroFormat(year); | |
| var month = zeroFormat(month); | |
| var day = zeroFormat(day); | |
| var triggerDate = year + month + day; | |
| /* YYYYMMDD形式をYYYY/MM/DD形式に変更 | |
| * | |
| * @param string | |
| * @param string | |
| * @return boolean | |
| */ | |
| function dateSeparate(targetDate){ | |
| var date = targetDate; | |
| if(parseInt(date.slice(0,4),10) < 2000 ){ | |
| var len = 2; | |
| } else { | |
| var len = date.length; | |
| } | |
| switch(len){ | |
| case 8: | |
| return date.slice(0,4) + '/' + date.slice(4,6) + '/' + date.slice(6,8); | |
| break | |
| case 4: | |
| return date.slice(4,6) + '/' + date.slice(6,8); | |
| break | |
| case 2: | |
| return date.slice(4,6); | |
| break | |
| default: | |
| return false; | |
| break | |
| } | |
| } | |
| console.log(dateSeparate(triggerDate)); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment