I am working with sailsjs(node.js), I have get all the data from mysql database and display the data in jtable but date format is coming like this:
YYYY-MM-DDTHH:mm:ss.000Z
.
I need to convert this(YYYY-MM-DDTHH:mm:ss.000Z) format into `14-08-2015 04:36:04 PM
In jtable i have used following format but not working.
UpdatedDate: {
edit: false,
create: false,
type: 'datewithtime',
displayFormat: 'dd-mm-yy',
Weightage: 2,
tooltip: 'Date Modified',
title: 'Date Modified'
/*display: function (data) {
return moment(data).format('YYYY-MM-DDTHH:mm:ss');
// return data.format("dd-m-yy");
}*/
}
in model
CreatedDate : { type: 'DATETIME'},
This is a simple workaround using just javascript. This may not be the best way to do this but its simple and works.
var date = new Date("1994-11-05T08:15:30-05:00");//this is the format you have
date.toLocaleDateString().split('/').join('-')+" "+date.toLocaleTimeString() //this converts it to necessary format
OUTPUT: "11-5-1994 6:45:30 PM"
EDIT: Sorry for that I guess it converted date as per your System Locale. Ok well so raw fix is as follows ::
var date = new Date("1994-11-05T08:15:30-05:00");//this is the format you have")
var datePart = date.getDate()+"-"+date.getMonth()+"-"+date.getFullYear();
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var secs = date.getSeconds();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes +':'+secs+ ' ' + ampm;
return strTime;
}
var timePart = formatAMPM(date);
console.log(datePart+" "+timePart);
OK this time each field was extracted individually rather than using any existing format so this will work for sure. Alternatively if you are open to using external libraries here is another solution using moment.js
moment(date).format("DD-MM-YYYY hh:mm:ss A")