日期格式由“DD / MM / YYYY”自动更改为“MM / DD / YYYY” [英] date format automatically changed from 'dd/mm/yyyy' to 'mm/dd/yyyy'

查看:216
本文介绍了日期格式由“DD / MM / YYYY”自动更改为“MM / DD / YYYY”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个JavaScript函数:

so i have this javascript function:

<script  type="text/javascript">

function CompareDates(id)
{

var monName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");

var d = new Date(id);

var curr_date = d.getDate();

var curr_month = d.getMonth();

var curr_year = d.getFullYear();


return (curr_date + " " + monName[curr_month] + " " + curr_year);
}

</script>

再此日历标记:

<p:calendar

id="testDate"

styleClass="calendar" 

pattern="d MMM, yyyy"

maxlength="10"

onchange="$(this).val(CompareDates($(this).val()))"

onfocus="$(this).mask('99/99/9999');"

>

<p:watermark for="testDate" value="mm/dd/yyyy" />

</p:calendar>

和一些未知的原因,该文本框接受默认的日期格式是在MM / DD / YYYY

and for some unknown reason, the default date format that the textbox accepts is in 'mm/dd/yyyy'

例如,如果我进入13-05-2014,然后它会返回一个错误,说明日期的无效。

For example if i entered "13-05-2014" then it would return an error stating date is invalid.

如果我进入2014年12月5日,然后它会返回2014年12月5日

If i entered "12-05-2014" then it would return "5 Dec, 2014"

我没有宣布任何日期格式anywhre除了其中如上图所示datepickers,为'D MMM,YYYY

I did not declare any dateformat anywhre except for the datepickers which as shown above, is 'd MMM, yyyy'

在发生这种情况我造模和错误许多不同种类的codeS尝试但它来验证日期都没有工作,所以我恢复了它全部回到原来的codeS。

Before this happened i trialed and error many different kind of codes to try to validate the date however it all didnt work and so i reverted it all back to the original codes.

最后一次的日期格式所接受的文本框为DD / MM / YYYY它工作得很好,除了验证部分我的JavaScript函数。

Last time the dateformat that the textbox accepted was 'dd/mm/yyyy' and it worked fine with my javascript function except the validation part.

现在它仍然工程除了日期格式更改为MM / DD / YYYY

Now it still works except that the dateformat changed to 'mm/dd/yyyy'.

我曾尝试使用的console.log找出什么是错的,但没有错误消息。

I did try to use console.log to find out what's wrong but there were no error messages.

所以,任何人都可以告诉我,告诉我是怎么回事呢?

So could anyone tell me and tell me what is going on here?

为什么的日期格式本身改变?

Why has the dateformat changed by itself?

感谢你在前进!

推荐答案

当你这样做:

var d = new Date(id);

您有效地调用 date.parse 时,这在很大程度上是依赖于实现的。不幸的是在大多数浏览器默认解析器将字符串在这样的格式NN / NN / NNNN是月/日/年。

you are effectively calling date.parse, which is largely implementation dependent. Unfortunately the default parser in most browsers expects strings in a format like nn/nn/nnnn to be month/day/year.

您可以通过手动解析字符串避免这种情况。如果你的字符串是D / M / Y则:

You can avoid that by manually parsing the string. If your string is d/m/y then:

function parseDMY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], --b[1], b[0]);
}

您也应该提供一个屏幕上的提示,告诉用户使用什么格式。

You should also provide an on screen tip to tell users what format to use.

这篇关于日期格式由“DD / MM / YYYY”自动更改为“MM / DD / YYYY”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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