Shaddy
Shaddy

Reputation: 212

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

Upvotes: 1

Views: 338

Answers (4)

Shaddy
Shaddy

Reputation: 212

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>

Upvotes: 0

jameshwart lopez
jameshwart lopez

Reputation: 3131

You can use jquery-dateFormat jquery plugin.

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

Upvotes: 0

vsogrimen
vsogrimen

Reputation: 456

try 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:

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>

Upvotes: 1

zambesianus
zambesianus

Reputation: 1249

Jquery UI has a date parser you can use that.

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

Upvotes: 0

Related Questions