如何在javascirpt中更改日期格式 [英] how to change date format in javascirpt

查看:86
本文介绍了如何在javascirpt中更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期是 2014-02-20T22:04:58.797 格式,但我希望日期在

02/20/2014 10:04 pm 。我的代码是

I am getting date in 2014-02-20T22:04:58.797 format but i want date in
02/20/2014 10:04 pm.My code is

function dispose() {
           if (currentSelectedRows != null) {
               var date = currentSelectedRows.data.DispDate;
               str = "<table cellspacing="0" cellpadding="0" style="width:75%">";
               str += + "<tr style="height:15px"><th align="left" style="font-weight:bold; color:black; font-size:12px; height:15px;">Ir user wrote :</th><td style="text-align:left; font-size:12px;">" + currentSelectedRows.data.DispDate + "</td></tr>"
               +"</table>";
           }

       }



我来这里 dispdate = 2014-02-20T22 :04:58.797 我希望日期在 02/20/2014 10:04 pm 格式。

请帮忙我。


Here i am getting dispdate = 2014-02-20T22:04:58.797 and i want date in 02/20/2014 10:04 pm format.
Please help me.

推荐答案

有多个库(Date.js,moment.js和其他)来处理这个......

此外,很容易找到谷歌搜索,请在下次询问之前做好尽职调查。







如果你想要自己的功能写它。这样的事情:

There are multiple libraries (Date.js, moment.js and others) that take care of this...
Also, it is easy to find googling, please do due dilligence next time before asking.



If you want your own function write it. Something like this:
<script type="text/javascript">
    var d = new Date();
    var curr_day = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    document.write(curr_day+ "/" + curr_month + "/" + curr_year);
</script>





有getHour和其他人可以得到精确的价值你需要。





如果这有帮助请花点时间接受解决方案。



There are getHour and others to get however precise value you need.


If this helps please take time to accept the solution.


最好的我到目前为止使用的是 Moment.js [ ^ ]。



- 下载脚本。



- 添加将它引用到页面并准备好了。



下面的代码应该适合你。
The best one i used so far is Moment.js[^].

- Download script.

- Add reference of it to the page and you are ready to go.

Below code should work for you.
var Available_Date = "2014-02-20T22:04:58.797"; 

var Required_Date = moment(Available_Date).format("MM/DD/YYYY hh:mm a");



永远记住,日期不从来没有任何格式。您可以将日期转换为您需要的格式,但结果将是字符串,而不是日期。



在我们的例子中, Required_Date 的字符串表示形式Available_Date



问候..


Always remember, Date doesn't have any format,never. You can convert date into format you require,but that result will be string,not Date.

In our case, Required_Date is string representation of Available_Date

Regards..


检查以下链接



http://forums.asp.net/t/1909901.aspx?How+to+convert+date+in+format+dd+MM+yyyy+in+javascript [ ^ ]
check the following link

http://forums.asp.net/t/1909901.aspx?How+to+convert+date+in+format+dd+MM+yyyy+in+javascript[^]


这篇关于如何在javascirpt中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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