Javascript dd.MM.yyyy 格式的日期

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

Format Date in dd.MM.yyyy

javascript

提问by cuongle

Possible Duplicate:
Javascript add leading zeroes to date

可能的重复:
Javascript 添加前导零到日期

It might be a simple question because I am still newbiein JavaScript, assume I have DateTime in ISO format:

这可能是一个简单的问题,因为我仍然是 JavaScript新手,假设我有 ISO 格式的 DateTime:

 2012-07-07T17:00:00

I would like to format this date to string:

我想将此日期格式化为字符串:

 07.07.2012

I have written a function to format to 7.7.2012 as below:

我编写了一个函数来格式化为 7.7.2012,如下所示:

var formatDate = function (datum) {
    var date = new Date(datum);
    return date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
};

How can I modify this code to get the result 07.07.2012instead of 7.7.2012

如何修改此代码以获取结果07.07.2012而不是7.7.2012

回答by polin

This might be helpful.

这可能会有所帮助。

<script type="text/javascript">

    var date=new Date();
    day=date.getDate();
    month=date.getMonth();
    month=month+1;
    if((String(day)).length==1)
    day='0'+day;
    if((String(month)).length==1)
    month='0'+month;

    dateT=day+ '.' + month + '.' + date.getFullYear();
    //dateT=String(dateT);
    alert(dateT);
</script>

回答by Mandeep Jain

You can also take a look at this

你也可以看看这个

Moment.js

时刻.js

Its the best I found, and it also has a host of other useful functions.

它是我发现的最好的,它还有许多其他有用的功能。

回答by 75inchpianist

use this handy script. The link provides instructions

使用这个方便的脚本。该链接提供说明

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

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