如何格式化到d,MMM yyyy在javascript [英] How to format datestring to d, MMM yyyy in javascript

查看:109
本文介绍了如何格式化到d,MMM yyyy在javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个:

function parseDate(s)
{
var d = s.split(/\D/);


return new Date(d[2], --d[1], d[0]);

与这样的日历标签:

<p:calendar
id="testDate"
styleClass="calendar"
pattern="d MMM, yyyy"
maxlength="10"
onchange="$(this).val(parseDate($(this).val()))"
onfocus="$(this).mask('99/99/9999');"
>
<p:watermark for="testDate" value="mm/dd/yyyy" />
</p:calendar>

我需要手动解析一个日期从'dd / mm / yyyy'到'd,MMM yyyy '但是在上面的功能上,结果就是例如Wed,Aug 09 1995 00:00:00因此,有人可以帮助我,告诉我如何更改格式,以便生成的字符串将是 d,MMM yyyy

I need to manually parse a date from 'dd/mm/yyyy' to 'd, MMM yyyy' but with the function above the result is e.g. "Wed, Aug 09 1995 00:00:00" hence could someone help me and tell how how i could change the format so that the string produced would be d, MMM yyyy?

我知道这应该是一个非常基本的任务,但是我仍然在学习如何编写更好的代码,因此所有的帮助和解释都非常感谢!

I know this should be a very basic task however i am still learning how to code better, therefore all your help and explanations are greatly appreciated!

推荐答案

Lazy方法:使用 .toString()

Lazy method: use the .toString()

function parseMyDate(s){

    var dateParts = s.split("/");

    var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

    var dateStrParts = date.toString().split(" ");

    return (date.getDate() + ", " + dateStrParts[1] + " " + dateStrParts[3]);
}

console.log(parseMyDate("09/06/2014"));

JSFiddle Demo

这篇关于如何格式化到d,MMM yyyy在javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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