使用JavaScript格式化日期 [英] Formatting the Date in JavaScript

查看:99
本文介绍了使用JavaScript格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java脚本中使用newDate()函数,我可以获得今天的日期。我的日期格式为3/3/2009(d / m / yyyy)。但实际上我需要的日期格式为2009-03-03(yyyy-mm-dd)。任何人都可以让我知道如何按照我的要求格式化日期?

Using newDate() function in Java script, I am able to get today's date. I am getting the date in the format 3/3/2009 (d/m/yyyy). But i actually need the date in the format 2009-03-03 (yyyy-mm-dd). Can anyone pls let me know how to format the date as i require?

推荐答案

你通常必须编写自己的函数来处理日期的格式化为JavaScript不包括用户定义的方式格式化日期的好方法。你可以在网上找到一些很棒的代码,因为这样做是为了死亡,尝试这样做:

You usually have to write your own function to handle the formatting of the date as javascript doesn't include nice methods to format dates in user defined ways. You can find some nice pieces of code on the net as this has been done to death, try this:

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

编辑:上面的代码似乎是非常好的,并且通过日期对象的原型安装了一个很酷的格式方法。我会用那个。

The above code seems to be really nice, and installs a cool 'format' method via the date object's prototype. I would use that one.

这篇关于使用JavaScript格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆