简体   繁体   English

如何将日期转换为长日期?

[英]How to convert a date to long date?

How to convert a date(01-02-2019) to Wed, 02 Jan 2019 in javascript? 如何在javascript中将日期(01-02-2019)转换为2019年1月2日星期三?

 $(document).ready(function () {
       var dealDate = 01-02-2019;
});

Just use new Date() on that date value: 只需在该日期值上使用new Date()

 $(document).ready(function() { var dealDate = '01-02-2019'; //replace all - to / to make it work on firefox dealDate = dealDate.replace(/-/g,'/'); alert(new Date(dealDate).toDateString()) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You can use Date Constructor and Date.prototype.toDateString() : 您可以使用Date ConstructorDate.prototype.toDateString()

The toDateString() method returns the date portion of a Date object in human readable form in American English. toDateString()方法以美国可读的形式返回Date对象的日期部分。

 $(document).ready(function () { var dealDate = new Date('01-02-2019').toDateString(); console.log(dealDate); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You can split your string on - and then generate the date using Date constructor. 您可以在拆分您的字符串-然后生成date使用Date构造函数。

 var dealDate = '01-02-2019'; let [month, day, year] = dealDate.split('-').map(Number); let date = new Date(year, month - 1, day); console.log(date.toDateString()); 

TO convert the date to in the format of month day, month day number and year use the below jquery. 要将日期转换为月日,月日编号和年份的格式,请使用以下jquery。 It will convert the current date to the exact format you asked for 它将当前日期转换为您要求的确切格式

$(document).ready(function() {
var dealDate = new Date();
alert(dealDate.toUTCString());
});

your expected format is like [day][comma][date][month][year]. 您的预期格式为[day] [逗号] [date] [month] [year]。 I split toDateString() and rearranged in expected format. 我拆分了toDateString()并以预期的格式重新排列。

  function formatedDate(d){ var dt=new Date(d).toDateString().split(' '); return dt[0]+', '+dt[2]+' '+dt[1]+' '+dt[3]; //[Day][comma][date][month][year] } console.log(formatedDate('01-02-2019')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM