将 MM/DD/YYYY 日期转换为月日年

Convert MM/DD/YYYY date to Month Day Year

我正在尝试将 MM/DD/YYYY 日期转换为长日期。因此,例如,02/12/2013 将转换为类似 Tuesday February 12 2013 的内容。

我看过 MomentJS 和其他 JS 方法,但没有一个真正达到我想要的效果。或者至少,我不这么认为。

有没有办法使这个日期转换准确?

有了moment.js就很简单了:

console.log(moment('02/12/2013', 'MM/DD/YYYY').format('dddd MMMM D Y'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>

使用时刻,

moment().format('dddd MMMM Do YYYY');

在 momentJS 的主页上,您可以看到所有可能的日期格式列表 (http://momentjs.com/)。

Do在日期上加上"nth",使D只显示一个数字,但是因为你都是用文字写的,所以最好使用"nth"

您可以使用普通的 Javascript 日期方法来处理类似的事情:

var date  = new Date("01/01/2000")
date.toDateString()   //"Sat Jan 01 2000"

完整JS:

function mydate(date)
{
  var
    month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    days  = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  ;
  return days[date.getDay()]+' '+month[date.getMonth()]+' '+date.getDate()+' '+date.getFullYear()
}

console.log(mydate(new Date()));

参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

如果您不需要任何其他脚本,您可以使用 Date 和一些数组

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var now = new Date('02/12/2013');
console.log(days[now.getDay()] + ' ' + months[now.getMonth()] + ' ' + now.getDate() + ' ' + now.getFullYear()); //Tuesday February 12 2013

使用moment.js,

你可以像这样使用 JavaScript 日期对象

var date = new Date(2013, 1, 12);
console.log(moment(date).format('dddd MMMM D Y'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>

如果要将日期字符串转换为长日期格式字符串,可以这样做。

var longDateStr = moment('02/12/2013', 'M/D/Y').format('dddd MMMM D Y');
console.log(longDateStr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>

在现代浏览器上,您可以使用 Intl API

The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The constructors for Collator, NumberFormat, and DateTimeFormat objects are properties of the Intl object.

手动解析,Date.parse

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

Date.UTC is used to provide a date from parts for Intl.DateTimeFormat#format.

The Date.UTC() method accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time

最后,, 被从 en-US 格式的字符串中删除,以便它符合您的要求。

const parts = '02/12/2013'.split('/');
parts.unshift(parts.pop());
parts[1] -= 1;
const dateString = new Intl.DateTimeFormat('en-US', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  timeZone: 'UTC'
}).format(Date.UTC(...parts)).replace(/,/g, '');
console.log(dateString);

我认为有一些简单的方法可以做到这一点。

function getData() {
  var d = new Date("02/12/2013");
  var options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  };
  var n = d.toLocaleDateString('en-US', options);
  var t = d.toLocaleTimeString('en-GB')
  var replase = n.replace(new RegExp(',', 'g'), ' ')
  document.getElementById("demo").innerHTML = replase
}
getData()
<span id="demo"></span>