我必须使用Jquery格式化日期,我尝试使用Date.js和一些外部JS,但无法这样做:
Input value:
Date val: 20150306
Time :185900
Expected format:
time: 6.59 PM
Date :Mar 06, 2015
答案 0 :(得分:1)
这是我的版本(未经测试)
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
date = "20150306",
time = "185900",
//http://www.w3schools.com/jsref/jsref_slice_string.asp
date_year = date.slice(0, 4),
date_month = date.slice(4, 6),
date_day = date.slice(6, 8),
time_hours = time.slice(0, 2),
time_mins = time.slice(2, 4),
time_secs = time.slice(4, 6),
//http://www.w3schools.com/jsref/jsref_obj_date.asp
dateObj = new Date(date_year, date_month, date_day, time_hours, time_mins, time_secs, 0),
dateString = [monthNames[dateObj.getMonth() - 1], " ", dateObj.getDate(), ", ", dateObj.getFullYear()].join(""),
//Bit of Math
timeHours = dateObj.getHours() % 12 || 12,
timeSuffix = time_hours >= 12 ? 'PM' : 'AM',
timeString = [timeHours, ':', time_mins, ' ', timeSuffix].join("");
console.log("Date: " + dateString);
console.log("Time: " + timeString);