我有一个日期,格式是2014年5月11日太阳。如何使用JavaScript将其转换为2014-05-11 ?

函数taskDate(dateMilli) { var d = (new Date(dateMilli) + ")。分割(' '); D [2] = D [2] + ','; 返回[d[0], d[1], d[2], d[3]]。加入(' '); } var datemilli =日期。解析(' 2014年5月11日'); console.log (taskDate (datemilli));

上面的代码给了我相同的日期格式,2014年5月11日。我该如何解决这个问题?


当前回答

这是我必须改变的关于人们从格林威治标准时间向东或向西的偏移量:

export const toNativeHtml5InputDate = (date) => {
  if (!date) return date;

  let offset = new Date(date).getTimezoneOffset();

  offset =
    offset < 0
      ? offset * -1 // east from Greenwich Mean Time
      : offset; // west from Greenwich Mean Time

  return new Date(new Date(date).getTime() + offset * 60 * 1000)
    .toISOString()
    .split('T')[0];
};

其他回答

你可以试试这个:https://www.npmjs.com/package/timesolver

npm i timesolver

在你的代码中使用它:

const timeSolver = require('timeSolver');
const date = new Date();
const dateString = timeSolver.getString(date, "YYYY-MM-DD");

你可以使用下面的方法获取日期字符串:

getString

PHP兼容的日期格式

下面是一个小函数,它可以接受与PHP函数date()相同的参数,并在JavaScript中返回日期/时间字符串。

注意,并不是PHP中的所有date()格式选项都受支持。您可以扩展parts对象来创建缺少的格式令牌

/** * Date formatter with PHP "date()"-compatible format syntax. */ const formatDate = (format, date) => { if (!format) { format = 'Y-m-d' } if (!date) { date = new Date() } const parts = { Y: date.getFullYear().toString(), y: ('00' + (date.getYear() - 100)).toString().slice(-2), m: ('0' + (date.getMonth() + 1)).toString().slice(-2), n: (date.getMonth() + 1).toString(), d: ('0' + date.getDate()).toString().slice(-2), j: date.getDate().toString(), H: ('0' + date.getHours()).toString().slice(-2), G: date.getHours().toString(), i: ('0' + date.getMinutes()).toString().slice(-2), s: ('0' + date.getSeconds()).toString().slice(-2) } const modifiers = Object.keys(parts).join('') const reDate = new RegExp('(?<!\\\\)[' + modifiers + ']', 'g') const reEscape = new RegExp('\\\\([' + modifiers + '])', 'g') return format .replace(reDate, $0 => parts[$0]) .replace(reEscape, ($0, $1) => $1) } // ----- EXAMPLES ----- console.log( formatDate() ); // "2019-05-21" console.log( formatDate('H:i:s') ); // "16:21:32" console.log( formatDate('Y-m-d, o\\n H:i:s') ); // "2019-05-21, on 16:21:32" console.log( formatDate('Y-m-d', new Date(2000000000000)) ); // "2033-05-18"

Gist

以下是formatDate()函数的更新版本和其他示例的要点:https://gist.github.com/stracker-phil/c7b68ea0b1d5bbb97af0a6a3dc66e0d9

如果你使用momentjs,现在它们包含了一个YYYY-MM-DD格式的常量:

date.format(moment.HTML5_FMT.DATE)

Date.js很适合这个。

require("datejs")
(new Date()).toString("yyyy-MM-dd")

2020的答案

你可以使用本地的. tolocaledatestring()函数,它支持一些有用的参数,如区域设置(选择MM/DD/YYYY或YYYY/MM/DD格式),时区(转换日期)和格式详细选项(例如:1 vs 01 vs一月)。

例子

const testCases = [ new Date().toLocaleDateString(), // 8/19/2020 new Date().toLocaleString(undefined, {year: 'numeric', month: '2-digit', day: '2-digit', weekday:"long", hour: '2-digit', hour12: false, minute:'2-digit', second:'2-digit'}), new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}), // 08/19/2020 (month and day with two digits) new Date().toLocaleDateString('en-ZA'), // 2020/08/19 (year/month/day) notice the different locale new Date().toLocaleDateString('en-CA'), // 2020-08-19 (year-month-day) notice the different locale new Date().toLocaleString("en-US", {timeZone: "America/New_York"}), // 8/19/2020, 9:29:51 AM. (date and time in a specific timezone) new Date().toLocaleString("en-US", {hour: '2-digit', hour12: false, timeZone: "America/New_York"}), // 09 (just the hour) ] for (const testData of testCases) { console.log(testData) }

注意,有时要以特定的格式输出日期,必须找到与该格式兼容的区域设置。 您可以在这里找到本地示例:https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring_date_all

请注意locale只是改变格式,如果你想将特定日期转换为特定国家或城市的等效时间,那么你需要使用timezone参数。