javascript JS:new Date() 在我自己的语言环境中不接受日期字符串 (d/m/y)

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

JS: new Date() is not accepting date string in my own locale (d/m/y)

javascriptlocaledate

提问by ingredient_15939

My browser (ie. my OS) should know I'm in Australia and what the correct date format is. In this case, d/m/y, not m/d/y. However if I run the following code:

我的浏览器(即我的操作系统)应该知道我在澳大利亚以及正确的日期格式是什么。在这种情况下,是 d/m/y,而不是 m/d/y。但是,如果我运行以下代码:

alert(new Date("21/11/1968"))

The result is "Thu Sep 11 1969". It is thinking the month comes first and adjusting accordingly.

结果是“1969 年 9 月 11 日星期四”。它认为月份在前并相应地进行调整。

Why is this? Is the answer to always use a universal format as input to date functions, or is there a way to tell the browser to expect dates input in my locale format?

为什么是这样?始终使用通用格式作为日期函数输入的答案,还是有办法告诉浏览器期望以我的语言环境格式输入日期?

采纳答案by Joe

The Date object is very weak. You cannottell it what format to expect. You can create it with a string in m/d/y like you stated, or new Date(year, month, day[, hours, seconds, milliseconds]);

Date 对象非常弱。你不能告诉它期望什么格式。您可以像您说的那样使用 m/d/y 中的字符串创建它,或者new Date(year, month, day[, hours, seconds, milliseconds]);

回答by KooiInc

It's pretty simple to convert your date string to a format that will give the expected result ('yyyy/mm/dd' or 'yyyy-mm-dd'):

将日期字符串转换为可提供预期结果的格式('yyyy/mm/dd' 或 'yyyy-mm-dd')非常简单:

new Date("21/11/1968".split('/').reverse().join('/'));

[edit] You may like this more generic method (part of the npm PureHelpers library):

[编辑] 你可能喜欢这个更通用的方法(npm PureHelpers 库的一部分):

document.querySelector("#result").textContent = `
  tryParseDate("2017/03/22", "ymd"); // ${tryParseDate("2017/03/22", "ymd")}
  tryParseDate("03/22/2017", "mdy"); // ${tryParseDate("03/22/2017", "mdy")}
  tryParseDate("22-03-2017", "dmy"); // ${tryParseDate("22-03-2017", "dmy")}
`;

function tryParseDate(dateStringCandidateValue, format = "dmy") {

  if (!dateStringCandidateValue) {
      return null;
  }
  
  const mapFormat = format.split("").reduce(function(a, b, i) {
      a[b] = i;
      return a;
  }, {});
  const dateStr2Array = dateStringCandidateValue.split(/[ :\-\/]/g);
  const datePart = dateStr2Array.slice(0, 3);
  const datePartFormatted = [
    +datePart[mapFormat.y], 
    +datePart[mapFormat.m] - 1, 
    +datePart[mapFormat.d]
  ];
  
  if (dateStr2Array.length > 3) {
    dateStr2Array.slice(3).forEach(t => datePartFormatted.push(+t));
  }
  
  const dateTrial = new Date(Date.UTC.apply(null, datePartFormatted));
  return dateTrial && dateTrial.getFullYear() === datePartFormatted[0] &&
          dateTrial.getMonth() === datePartFormatted[1] &&
          dateTrial.getDate() === datePartFormatted[2] 
      ? dateTrial 
      : null;
}
<pre id="result"></pre>

回答by Arkadiusz Cie?liński

new Date(string_date) supports the following Date formats:

new Date(string_date) 支持以下日期格式:

  1. MM-dd-yyyy
  2. yyyy/MM/dd
  3. MM/dd/yyyy
  4. MMMM dd, yyyy
  5. MMM dd, yyyy
  1. MM-dd-yyyy
  2. 年年/月/日
  3. 月/日/年
  4. MMMM dd, yyyy
  5. MMM dd, yyyy