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

时间:2018-12-31 09:18:16

标签: javascript jquery

如何使用javascript将日期(01-02-2019)转换为2019年1月2日星期三?

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

5 个答案:

答案 0 :(得分:2)

只需在该日期值上使用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>

答案 1 :(得分:1)

您可以使用Date ConstructorDate.prototype.toDateString()

  

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>

答案 2 :(得分:1)

您可以在-上拆分字符串,然后使用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());

答案 3 :(得分:0)

要将日期转换为月日,月日数字和年份的格式,请使用以下jquery。它将当前日期转换为您要求的确切格式

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

答案 4 :(得分:0)

您的预期格式为[day] [逗号] [date] [month] [year]。我拆分了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'))

相关问题