简体   繁体   English

jQuery日期格式问题

[英]JQuery date formatting issue

I have to format date as below using Jquery, I tried with Date.js and some more external JS but unable to do so: 我必须使用Jquery格式化日期,如下所示,我尝试使用Date.js和其他一些外部JS,但无法这样做:

Input value: 
Date val: 20150306 
Time :185900


Expected format:
time:  6.59 PM 
Date :Mar 06, 2015

Here is my version (untested) 这是我的版本(未经测试)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM