[英]Change date format (Javascript)
I have this code: 我有以下代码:
var fd=1+self.theDate.getMonth() +'/'+ today+'/'+self.theDate.getFullYear();
It works, but it's format is Month, Day, Year. 可以,但是格式是月,日,年。
I need to change it to: Day, Month Year. 我需要将其更改为:Day,Month Year。
So, I tried this: 所以,我尝试了这个:
var fd=1+today +'/'+ self.theDate.getMonth()+'/'+self.theDate.getFullYear();
Now, my change does not work. 现在,我的更改不起作用。 Is it that I have not done it properly or is my change right?
是我做得不好还是找对了?
Thanks 谢谢
I expect the correct answer is this: 我期望正确的答案是这样的:
var fd=today +'/'+ (self.theDate.getMonth() + 1) +'/'+self.theDate.getFullYear();
This leaves today alone, and groups Month so that it does a proper number addition instead of string concatenation. 这样就剩下今天,对Month进行分组,以便它执行适当的数字加法运算而不是字符串连接。
var theDate = new Date();
var today = theDate.getDate();
var month = theDate.getMonth()+1; // js months are 0 based
var year = theDate.getFullYear();
var fd=today +'/'+ month +'/'+year
or perhaps you prefer 22/05/2011 或者您更喜欢22/05/2011
var theDate = new Date();
var today = theDate.getDate();
if (today<10) today="0"+today;
var month = theDate.getMonth()+1; // js months are 0 based
if (month < 10) month = "0"+month;
var year = theDate.getFullYear();
var fd=""+today +"/"+ month +"/"+year
You are no longer adding 1
to the month, you are adding it to today
. 您不再将
1
添加到月份,而是将其添加到today
。 Make sure to parenthesize this since "x" + 1 + 2 => "x12"
but "x" + (1 + 2) => "x3"
确保加上括号,因为
"x" + 1 + 2 => "x12"
但是"x" + (1 + 2) => "x3"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.