Javascript 新日期()格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10892154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:27:15  来源:igfitidea点击:

new date() formatting

javascriptdate

提问by Phallacy

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".

我已经坚持了一段时间,需要一些帮助。
我有一个使用"new date()".
除了需要它来显示 yyyy MMM dd hh:mm:ss 外,它工作得很好。
它当前显示“ Thu May 31 2012 13:04:29 GMT-0500 (CDT)”。
我需要它看起来像“ 2012 May 31 13:04:29”。

Any help would be awesome! Thanks

任何帮助都是极好的!谢谢

回答by Jason Kulatunga

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

我喜欢在处理大量复杂和不同的日期格式时使用moment.js,但这也适用于您:

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

..edit.. 如果您使用 moment.js,这将是多么简单

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

回答by Ja?ck

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

使用标准 JavaScript,您必须依赖Date 对象为您提供的内容:

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

您可能需要填充个位数,但这是读者练习:)

回答by Nikhil D

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

回答by panda-34

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

您可以对日期的字符串表示执行以下操作:

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

回答by COLD TOLD

here are couple of links that may help you

这里有几个链接可以帮助你

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

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

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

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

or using pure javascript you can do this

或者使用纯 javascript 你可以做到这一点

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;

回答by Addicted

SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd hh:mm:ss");
Date dated = format.parse(format.format(new Date()));