Question

I'm getting a date result from an API in the format yyyy-mm-dd (plus unwanted hh-mm-ss). I want to convert this to a dd-mm-yyyy format in jQuery, whats the easiest/quickest way?

At the moment I'm using: (for returned string yyyy-mm-dd):

var year = results.job.posted.substr(0,4);
var month = results.job.posted.substr(5,2);
var day = results.job.posted.substr(8,2);
$('#job-date').text(day+'-'+month+'-'+year);

but it seems a bit long winded.

Was it helpful?

Solution

Assuming you want it as a string in that format just chop it up and rearrange;

var d = "yyyy-mm-dd XXX";
d = d.substr(0, 10).split("-");

d = d[2] + "-" + d[1] + "-" + d[0];

OTHER TIPS

var date_old //your old date in yyyy-mm-dd
var date = dateFormat(new Date(date_old), 'dd-mm-yyyy');

If you have date object then you can use

var date_Obj = new Date(last_modified);
var newDate = date_Obj .getFullYear() + "-" + date_Obj.getMonth() + "-" + date_Obj.getDate() + "T" + date_Obj.getHours() + ":" + date_Obj.getMinutes() + ":" + date_Obj.getSeconds()  

or you can follow Convert string to date in Javascript /Jquery

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top