我有一个日期,格式是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日。我该如何解决这个问题?


当前回答

这些答案没有一个让我很满意。我想要一个跨平台的解决方案,让我可以在不使用任何外部库的情况下使用本地时区的一天。

这是我想到的:

function localDay(time) {
  var minutesOffset = time.getTimezoneOffset()
  var millisecondsOffset = minutesOffset*60*1000
  var local = new Date(time - millisecondsOffset)
  return local.toISOString().substr(0, 10)
}

它应该在日期引用的时区中以YYYY-MM-DD格式返回日期的日期。

因此,例如,localDay(new Date("2017-08-24T03:29:22.099Z"))将返回"2017-08-23",即使它在UTC已经是24日。

你需要填充Date.prototype.toISOString,它才能在Internet Explorer 8中工作,但它应该在其他任何地方都能得到支持。

其他回答

如果日期需要在所有时区都相同,例如表示数据库中的某个值,那么请确保在JavaScript date对象上使用UTC版本的日、月、年函数,因为这将以UTC时间显示,并避免在某些时区出现差一的错误。

更好的是,使用Moment.js日期库来进行这种格式设置。

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

toISOString()假设日期是本地时间并将其转换为UTC。您将得到一个不正确的日期字符串。

下面的方法应该返回您需要的内容。

Date.prototype.yyyymmdd = function() {         

    var yyyy = this.getFullYear().toString();                                    
    var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based         
    var dd  = this.getDate().toString();             

    return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]);
};

来源:https://blog.justin.kelly.org.au/simple-javascript-function-to-format-the-date-as-yyyy-mm-dd/

这些答案没有一个让我很满意。我想要一个跨平台的解决方案,让我可以在不使用任何外部库的情况下使用本地时区的一天。

这是我想到的:

function localDay(time) {
  var minutesOffset = time.getTimezoneOffset()
  var millisecondsOffset = minutesOffset*60*1000
  var local = new Date(time - millisecondsOffset)
  return local.toISOString().substr(0, 10)
}

它应该在日期引用的时区中以YYYY-MM-DD格式返回日期的日期。

因此,例如,localDay(new Date("2017-08-24T03:29:22.099Z"))将返回"2017-08-23",即使它在UTC已经是24日。

你需要填充Date.prototype.toISOString,它才能在Internet Explorer 8中工作,但它应该在其他任何地方都能得到支持。

formatDate(date) {
  const d = new Date(date)
  const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
  const mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d);
  const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);
  return `${da}-${mo}-${ye}`;
}

console.log("Formatated Date : ", formatDate("09/25/2020") )
// Output :: Formatated Date : 25-Sep-2020