gpt4 book ai didi

javascript - JQuery日期格式问题

转载 作者:行者123 更新时间:2023-12-03 10:38:21 26 4
gpt4 key购买 nike

我必须使用 Jquery 格式化日期,我尝试使用 Date.js 和一些更多的外部 JS,但无法这样做:

Input value: 
Date val: 20150306
Time :185900


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

最佳答案

这是我的版本(未经测试)

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

关于javascript - JQuery日期格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com