Last active
May 19, 2018 10:56
-
-
Save yu-smc/93df5caac267676956e9164b79e96ff0 to your computer and use it in GitHub Desktop.
JavaScriptで取得した日付(年月日)を英語表記してみる (1行ごとに解説) ref: https://qiita.com/yu-smc/items/8bdde9ff6729ce848c09
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 get_english_date() { | |
const month_english_list = ['Jan.','Feb.','Mar.','Apr.','May','June','July','Aug.','Sept.','Oct.','Nov.','Dec.'] | |
//月の英語表記を配列に定義。省略形は不規則で、①Mayは3文字だからカンマはいらない、②June, Julyは4文字のまま、③9月は4文字でSept. 初めて知った。 | |
var date = new Date() | |
//Dateオブジェクトで日付を取得(引数を持たせなければ現在時刻を取得する) | |
var month = date.getMonth() | |
//dateから月を取り出してmonthに代入 | |
var month_english = month_english_list[month] | |
//month_english-listのmonth番目の要素をmonth_englishに代入。 ※getMonth()は0-11までの整数が返る(1月は0になる)ので、[month-1]とする必要はない | |
var day = date.getDate() | |
//一番最後の式を見やすくするため、dateから日を取り出してdayに代入 | |
var year = date.getFullYear() | |
//同様にdateから年を取り出してyearに代入 | |
var date_english = month_english + " " + day + ", " + year | |
//あとは順番通り並べるだけ。 => May 19, 2018 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment