jQuery 在网页上显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7042851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Display Date on Webpage
提问by Justin Erswell
I want to display this on a webpage using jQuery...
我想使用 jQuery 在网页上显示它...
Friday 12 August 2011
2011 年 8 月 12 日星期五
I have a div with an ID of date setup that I would like to print it into.
我有一个带有日期设置 ID 的 div,我想将其打印到其中。
Jquery so far..
Jquery到目前为止..
var newDate = new Date();
newDate.setDate(newDate.getDate() + 1);
$('#Date').html((newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear());
Can someone tell me how to format this correctly?
有人能告诉我如何正确格式化吗?
回答by ipr101
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var newDate = new Date();
newDate.setDate(newDate.getDate() + 1);
$('#Date').html(dayNames[newDate.getDay()] + " " + newDate.getDate() + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear());
Working demo - http://jsfiddle.net/ipr101/X9hyZ/
回答by Jon Erickson
Since your using jQuery, one of its' strengths is the plugin architecture.
由于您使用 jQuery,它的优势之一是插件架构。
So I'd just find and use a plugin for this
所以我只是为此找到并使用插件
http://plugins.jquery.com/project/jquery-dateFormat
http://plugins.jquery.com/project/jquery-dateFormat
http://pablocantero.com/blog/2010/09/04/jquery-plugin-javascript-for-java-util-date-tostring-format/
http://pablocantero.com/blog/2010/09/04/jquery-plugin-javascript-for-java-util-date-tostring-format/
var newDate = new Date();
newDate.setDate(newDate.getDate() + 1);
$('#Date').html($.format.date(newDate, 'ddd dd MMMM yyyy'));
回答by Jason Gritman
Though it's not a jQuery plugin, I've found the Date.js libraryto be extremely useful for all Date formatting and parsing in JavaScript. For your particular scenario, this code would output the formatted date:
虽然它不是 jQuery 插件,但我发现Date.js 库对于 JavaScript 中的所有日期格式和解析非常有用。对于您的特定场景,此代码将输出格式化日期:
Date.today().toString("dddd d MMMM yyyy")