使用jQuery或javascript转换日期格式(不使用任何lib)

时间:2016-01-13 04:43:26

标签: javascript php jquery datepicker

如何使用2016-01-05 04:06:5205 Jan,2016将此js日期格式转换为标准日期格式,例如jQuery。以下是我的代码。

var dateFormat = data.data[p].UserPackage.created;
alert(dateFormat); //2016-01-05 04:06:52


    var t = data.data[p].UserPackage.created.split(/[- :]/);
    alert(t); //2016,01,05,04,06,52

    var d = new Date(t[0], t[1] - 1, t[2], t[3], t[4], t[5]);

    alert(d); //Tue Jan 05 2016 04:06:52 GMT+0530 (India Standard Time)

我已经尝试了上面的编码,它的工作方式如何,但我希望确切的格式看起来像05 Jan,2016Jan 05 2016

4 个答案:

答案 0 :(得分:1)

尝试Moment.js

var dateFormat = '2016-01-05 04:06:52';
alert(moment(dateFormat).format('MMM YYYY'));

其他方式:

var date = moment().format('MMMM Do YYYY, h:mm:ss a'); // January 13th 2016, 12:53:43 pm
var date = moment().format('dddd');                    // Wednesday
var date = moment().format("MMM Do YY");               // Jan 13th 16
var date = moment().format('YYYY [escaped] YYYY');     // 2016 escaped 2016
var date = moment().format();                          // 2016-01-13T12:53:43+08:00

<强>样本:

&#13;
&#13;
var dateFormat = '2016-01-05 04:06:52';

var formatedDate = moment(dateFormat).format('DD MMM YYYY');

$('#result').html(formatedDate);

alert(formatedDate);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://momentjs.com/downloads/moment-with-locales.js"></script>

<p id="result"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用jquery-dateFormat jquery插件。

var  dateFormat=data.data[p].UserPackage.created;
 alert(dateFormat);//2016-01-05 04:06:52
document.write($.format.date(dateFormat, "dd MMM,yyyy"));

您也可以在不使用任何库的情况下转换它,就像下面的例子一样

            //Pure javascript codes    
            var datestr = "2016-01-05 04:06:52";

            var monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
            var date = new Date(datestr);
            var day = date.getDate() <10? "0"+date.getDate():date.getDate();
            var convertedDate = day+" "+monthNames[date.getMonth()]+","+ date.getFullYear();
            document.write(convertedDate);
            console.log(convertedDate);

答案 2 :(得分:0)

Jquery UI有一个可以使用它的日期解析器。

$.datepicker.parseDate( "dd MMM,yyyy", dateFormat );

答案 3 :(得分:0)

我已经找到了我的问题的解决方案,但是以一种冗长的方式。我把它放在一个函数中,从那里我只是调用它,通过传递所需的日期作为参数.Below是代码,我在调用直接。

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
     <script type="text/javascript">
         $(document).ready(function() {
          var datestr = "2016-01-05 04:06:52";
          var t = datestr.split(/[- :]/);
          replace_date = datestr.replace(' ', 'T');
          var date = new Date(replace_date);
          var month = date.getMonth() + 1;
          month = month < 10 ? '0' + month : month;
         if (month == "01") { month = "Jan"; }  else if (month == "02"){  month = "Feb"; }  else if (month == "03")  {month = "March";  } else if (month == "04")  {   month = "April";}else if (month == "05")    {   month = "May";  } else if (month == "06"){  month = "June";} else if (month == "07"){ month = "July";} else if (month == "08"){  month = "Aug";} else if (month == "09"){ month = "Sep";}else if (month == "10"){  month = "Oct";} else if (month == "11"){ month = "Nov";}else if (month == "12") {month = "Dec"; }
         alert(t[2] +' , ' + month +' '  + t[0]);
         });
    </script>
</head>
<body>
    
   
</body>
&#13;
&#13;
&#13;