更改日期格式(Javascript)

发布于 2024-11-09 03:12:17 字数 336 浏览 0 评论 0原文

我有这个代码:

var fd=1+self.theDate.getMonth() +'/'+ today+'/'+self.theDate.getFullYear();

它有效,但它的格式是月、日、年。

我需要将其更改为:日、月、年。

所以,我尝试了以下方法:

var fd=1+today +'/'+ self.theDate.getMonth()+'/'+self.theDate.getFullYear();

现在,我的更改不起作用。是我没做对还是我的改变对了?

谢谢

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⊕婉儿 2024-11-16 03:12:17

我预计正确的答案是这样的:

var fd=today +'/'+ (self.theDate.getMonth() + 1) +'/'+self.theDate.getFullYear();

这将单独保留今天,并将月份分组,以便它进行正确的数字加法而不是字符串连接。

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.

以可爱出名 2024-11-16 03:12:17
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

或者您可能更喜欢 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
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

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
怂人 2024-11-16 03:12:17

您不再将 1 添加到月份,而是将其添加到今天。确保将其括起来,因为 "x" + 1 + 2 => “x12”“x” + (1 + 2) => “x3”

You are no longer adding 1 to the month, you are adding it to today. Make sure to parenthesize this since "x" + 1 + 2 => "x12" but "x" + (1 + 2) => "x3"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文