Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ISOString to date format dd/mm/yyyy using javascript

I want to change the date to the format dd/mm/yyyy but I did not succeed. I tried something like this:

<span class="postupdate">2015-03-01T14:28:28Z</span>
$(function(){
    var d = $(".postupdate").html();
    var n = d.toLocaleDateString();
    d.html(n);
}); 

but I do not know how to to change the date it with javascript or jquery. whether to use a regex to change that date? Can someone help me?

like image 805
Dyana Putry Avatar asked Dec 28 '25 09:12

Dyana Putry


2 Answers

You can use regex to parsing values of date. In bottom code, i used regex in .match() to parsing day, month, year of date.

$(".postupdate").text().match(/([^T]+)/)[0].split("-").reverse().join("/");

In your case, you can use this code:

$("span").text(function(i, text){
    return text.match(/([^T]+)/)[0].split("-").reverse().join("/");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="postupdate">2015-03-01T14:28:28Z</span>
like image 191
Mohammad Avatar answered Dec 30 '25 23:12

Mohammad


 var d = $(".postupdate").html();
var date=new Date(d);

Then using X-Class package:

date.format('dd/mm/yyyy');
 // 01/03/2015

DEMO

like image 41
Abdennour TOUMI Avatar answered Dec 30 '25 22:12

Abdennour TOUMI