システム時刻に日付時刻を変換します。
HTML
<label class="grid_label_view system_datetime" data-datetime_formate="M d, Y H:i" data-gmt_datetime="2016-08-27 07:14:00">Aug 27, 2016 06:57</label>
<label class="grid_label_view system_datetime" data-datetime_formate="M d, Y h:i A" data-gmt_datetime="2016-08-27 06:57:00">Aug 27, 2016 06:57 AM</label>
<label class="grid_label_view system_datetime" data-datetime_formate="M d, Y h:i A" data-gmt_datetime="2016-08-27 07:14:00">Aug 27, 2016 07:14 AM</label>
はJavaScript:
$(".system_datetime[data-gmt_datetime][data-datetime_formate]").each(function() {
var gmt_datetime = $(this).attr("data-gmt_datetime");
var gmt_datetime_formate = $(this).attr("data-datetime_formate");
var js_gmt_datetime = new Date(gmt_datetime + ' GMT');
var current_gmt_time = new Date();
var datetime_timezoon_string = js_gmt_datetime.toString().match(/\(([A-Za-z\s].*)\)/)[1];
console.log("gmt_datetime:" + gmt_datetime + ",gmt_datetime_formate:" + gmt_datetime_formate);
var datetime_year_4_integer = js_gmt_datetime.getFullYear().toString();
console.log("datetime_year_4_integer:" + datetime_year_4_integer);
var datetime_year_2_integer = datetime_year_4_integer.toString().slice(-2);
var datetime_month_integer = (js_gmt_datetime.getMonth() + 1);
var datetime_month_full_string = 'NONE';
var datetime_month_short_string = 'NONE';
switch (datetime_month_integer) {
case 1:
datetime_month_full_string = "January";
datetime_month_short_string = "Jan";
break;
case 2:
datetime_month_full_string = "February";
datetime_month_short_string = "Feb";
break;
case 3:
datetime_month_full_string = "March";
datetime_month_short_string = "Mar";
break;
case 4:
datetime_month_full_string = "April";
datetime_month_short_string = "Apr";
break;
case 5:
datetime_month_full_string = "May";
datetime_month_short_string = "May";
break;
case 6:
datetime_month_full_string = "June";
datetime_month_short_string = "Jun";
break;
case 7:
datetime_month_full_string = "July";
datetime_month_short_string = "Jul";
break;
case 8:
datetime_month_full_string = "August";
datetime_month_short_string = "Aug";
break;
case 9:
datetime_month_full_string = "September";
datetime_month_short_string = "Sep";
break;
case 10:
datetime_month_full_string = "October";
datetime_month_short_string = "Oct";
break;
case 11:
datetime_month_full_string = "November";
datetime_month_short_string = "Nov";
break;
case 12:
datetime_month_full_string = "December";
datetime_month_short_string = "Dec";
break;
}
var datetime_day_integer = js_gmt_datetime.getDate().toString();
var datetime_hours_24_integer = js_gmt_datetime.getHours().toString();
var datetime_hours_12_integer = js_gmt_datetime.getHours().toString();
if (js_gmt_datetime.getHours() > 12) {
var datetime_hours_12_integer = "" + (js_gmt_datetime.getHours() - 12);
}
var datetime_minutes_integer = js_gmt_datetime.getMinutes().toString();
var datetime_seconds_integer = js_gmt_datetime.getSeconds().toString();
var datetime_small_am_pm_string = datetime_hours_24_integer >= 12 ? 'pm' : 'am';
var datetime_caps_am_pm_string = datetime_hours_24_integer >= 12 ? 'PM' : 'AM';
var system_datetime_formate = '';
datetime_day_integer = two_digit_convert(datetime_day_integer);
datetime_hours_24_integer = two_digit_convert(datetime_hours_24_integer);
datetime_hours_12_integer = two_digit_convert(datetime_hours_12_integer);
datetime_minutes_integer = two_digit_convert(datetime_minutes_integer);
datetime_seconds_integer = two_digit_convert(datetime_seconds_integer);
for (var char_index = 0; char_index < gmt_datetime_formate.length; char_index++) {
switch (gmt_datetime_formate[char_index]) {
case 'd':
system_datetime_formate = system_datetime_formate.concat(datetime_day_integer);
break;
case 'D':
system_datetime_formate = system_datetime_formate.concat("D");
break;
case 'j':
system_datetime_formate = system_datetime_formate.concat(datetime_day_integer.replace(/^0+/, ''));
break;
case 'F':
system_datetime_formate = system_datetime_formate.concat(datetime_month_full_string);
break;
case 'm':
system_datetime_formate = system_datetime_formate.concat(datetime_month_integer);
break;
case 'M':
system_datetime_formate = system_datetime_formate.concat(datetime_month_short_string);
break;
case 'n':
system_datetime_formate = system_datetime_formate.concat(datetime_month_integer.replace(/^0+/, ''));
break;
case 'Y':
system_datetime_formate = system_datetime_formate.concat(datetime_year_4_integer);
break;
case 'y':
system_datetime_formate = system_datetime_formate.concat(datetime_year_2_integer);
break;
case 'g':
system_datetime_formate = system_datetime_formate.concat(datetime_hours_12_integer.replace(/^0+/, ''));
break;
case 'G':
system_datetime_formate = system_datetime_formate.concat(datetime_hours_24_integer.replace(/^0+/, ''));
break;
case 'h':
system_datetime_formate = system_datetime_formate.concat(datetime_hours_12_integer);
break;
case 'H':
system_datetime_formate = system_datetime_formate.concat(datetime_hours_24_integer);
break;
case 'i':
system_datetime_formate = system_datetime_formate.concat(datetime_minutes_integer);
break;
case 'T':
system_datetime_formate = system_datetime_formate.concat(datetime_timezoon_string);
break;
case 'A':
system_datetime_formate = system_datetime_formate.concat(datetime_caps_am_pm_string);
break;
case 'a':
system_datetime_formate = system_datetime_formate.concat(datetime_small_am_pm_string);
break;
default:
system_datetime_formate = system_datetime_formate.concat(gmt_datetime_formate[char_index]);
}
}
$(this).html(system_datetime_formate);
});
function two_digit_convert(n) {
return n > 9 ? "" + n : "0" + n;
}
の
可能な重複[Iオフセットユーザーのロケールの形式と時間の日付/時刻を表示するにはどうすればよい?](のhttp:/ /stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset) – boesing