简体   繁体   English

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

[英]Convertion of date format using jQuery or javascript(Without using any lib)

How can i convert this 2016-01-05 04:06:52 format of date to a standard date format like 05 Jan,2016 using js or jQuery . 我如何使用jsjQuery将日期的2016-01-05 04:06:52日期格式转换为标准日期格式,如05 Jan,2016 Below is my code. 下面是我的代码。

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)

I have tried the above coding ,its working some how,but i want the exact format will look like 05 Jan,2016 or Jan 05 2016 . 我已经尝试了上述编码,其工作方式如何,但我希望确切的格式看起来像05 Jan,2016 Jan 05 201605 Jan,2016 Jan 05 2016

try Moment.js 试试Moment.js

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

Other Ways: 其他方法:

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

DEMO: DEMO:

 var dateFormat = '2016-01-05 04:06:52'; var formatedDate = moment(dateFormat).format('DD MMM YYYY'); $('#result').html(formatedDate); alert(formatedDate); 
 <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> 

You can use jquery-dateFormat jquery plugin. 您可以使用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"));

You can also convert it without the use of any library just like the example below 您也可以像下面的示例一样在不使用任何库的情况下进行转换

            //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);

jQuery UI有一个日期解析器,您可以使用它。

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

I have found the solution of my problem,but in a lengthy way.well i have put it inside a function,from there i am just calling it,by passing required date as argument.Below is code,where i am calling it directly. 我已经找到了解决我问题的方法,但是经过了很长的时间。好吧,我把它放在一个函数中,从那里我通过调用所需的日期作为参数来调用它。下面是代码,我直接在其中调用它。

 <!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> 

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

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