开发者

jQuery Display Date on Webpage

I want to display this on a webpage using jQuery...

Friday 12 August 2011

I have a div with an ID of date setup that I woul开发者_StackOverflow中文版d like to print it into.

Jquery so far..

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?


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/


Since your using jQuery, one of its' strengths is the plugin architecture.

So I'd just find and use a plugin for this

http://plugins.jquery.com/project/jquery-dateFormat

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

http://jsfiddle.net/HPhDV/


Though it's not a jQuery plugin, I've found the Date.js library to be extremely useful for all Date formatting and parsing in JavaScript. For your particular scenario, this code would output the formatted date:

Date.today().toString("dddd d MMMM yyyy")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜