最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - new date() formatting - Stack Overflow

programmeradmin3浏览0评论

I've been stuck on this for a while and need some help.
I have a date/time displayed using "new date()".
It works perfect except in need it to display yyyy MMM dd hh:mm:ss.
It currently displays "Thu May 31 2012 13:04:29 GMT-0500 (CDT)".
I need it to look like "2012 May 31 13:04:29".

Any help would be awesome! Thanks

I've been stuck on this for a while and need some help.
I have a date/time displayed using "new date()".
It works perfect except in need it to display yyyy MMM dd hh:mm:ss.
It currently displays "Thu May 31 2012 13:04:29 GMT-0500 (CDT)".
I need it to look like "2012 May 31 13:04:29".

Any help would be awesome! Thanks

Share Improve this question edited Jun 5, 2012 at 12:26 Addicted 1,7042 gold badges16 silver badges24 bronze badges asked Jun 5, 2012 at 5:40 PhallacyPhallacy 952 gold badges2 silver badges5 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 6

I love using moment.js when I'm doing lots of plex and different date formatting, but this should work for you too:

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

var mydate = new Date();
var curr_date = mydate.getDate();
var curr_month = mydate.getMonth();
var curr_year = mydate.getFullYear();

var mydatestr = '' + curr_year  + ' ' +
curr_month + ' ' + 
curr_date+ ' ' +
mydate.getHours() + ':' +
mydate.getMinutes() + ':' + 
mydate.getSeconds()

..edit.. Here's how simple it would be if you were using moment.js

var day = new Date()
var dayWrapper = moment(day); 
var dayString = dayWrapper.format("YYYY MMM D H:mm:ss"); 

Using standard JavaScript, you have to rely on what the Date object gives you:

var months = ['Jan', 'Feb', 'Mar', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
now = new Date(),
formatted = now.getFullYear() + ' ' + months[now.getMonth()] + ' ' + 
    now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + 
    now.getSeconds();

You may need to pad your single digits, but that's readers exercise :)

DateTime visitDate = DateTime.Parse("Thu May 31 2012 13:04:29");
        Label1.Text = visitDate.ToString("yyyy MMM dd HH:mm:ss");

you can execute the following on your date's string representation:

varname.replace(/^\S+\s(\S+\s\S+\s)(\S+\s)(\S+)\s.*$/, "$2$1$3");

here are couple of links that may help you

http://blog.stevenlevithan./archives/date-time-format

http://www.webdevelopersnotes./tips/html/javascript_date_and_time.php3

or using pure javascript you can do this

var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

var d = new Date();
var x = document.getElementById("demo");
var nmonth=month[d.getMonth()];
var nyear = d.getFullYear(); 
var nday = d.getDate();
var nhours = d.getHours()
var nminutes = d.getMinutes();
var nseconds=d.getSeconds();

var wholesyting=nyear+" "+nmonth+" "+nday+" "+nhours+":"+nminutes+":"+nseconds;
SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd hh:mm:ss");
Date dated = format.parse(format.format(new Date()));
发布评论

评论列表(0)

  1. 暂无评论