Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

javascript - Javascript格式日期/时间[重复](Javascript format date / time [duplicate])

This question already has an answer here:(这个问题在这里已有答案:)

Formatting the date time with Javascript 12 answers(使用Javascript 12答案 格式化日期时间)

I need to change a date/time from 2014-08-20 15:30:00 to look like 08/20/2014 3:30 pm(我需要将日期/时间从2014-08-20 15:30:00更改为2014年8月20日下午3:30)

Can this be done using javascript's Date object?(可以使用javascript的Date对象完成吗?)   ask by user2994560 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you can use the native javascript Date() object and its methods.(是的,您可以使用本机javascript Date() 对象及其方法。)

For instance you can create a function like:(例如,您可以创建一个函数,如:) function formatDate(date) { var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? '0'+minutes : minutes; var strTime = hours + ':' + minutes + ' ' + ampm; return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime; } var d = new Date(); var e = formatDate(d); alert(e); And display also the am / pm and the correct time.(并显示上午/下午和正确的时间。) Remember to use getFullYear() method and not getYear() because it has been deprecated.(请记住使用getFullYear()方法而不是getYear(),因为它已被弃用。) DEMO http://jsfiddle.net/a_incarnati/kqo10jLb/4/(演示 http://jsfiddle.net/a_incarnati/kqo10jLb/4/)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...