Reputation: 1436
I'm trying to format the date from something that looks like 26/05/2015 9:14:46 AM
to May 26.
I've managed to get the correct formatting on the current day, which is a good first step. However, in this case, I'm trying to format the date from the previous day, ie. the last time the API was updated regarding river levels var previousDate = result[1].Date;
I've tried var today = new Date(result[1].Date)
console logs out 22/05/2015 9:31:19 AM
and it returns me "undefined Nan"
$.ajax({
url: 'http://opengov.brandon.ca/OpenDataService/default.aspx?format=jsonp&dataset=riverlevel&columns=Date&callback=?',
type: 'GET',
dataType: 'jsonp',
success: function(result) {
// Dates
var currentDate = result[0].Date;
var previousDate = result[1].Date;
console.log(currentDate, previousDate);
// Change date from DD/MM/YYYY to January 18
// Create a new variable with full month names
var monthNames = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
// Puts everything above into a string
var fixedDate = monthNames[mm] + ' ' + dd;
$('.date').html(fixedDate); // This presents the fixed current date
Upvotes: 0
Views: 676
Reputation: 3729
As pointed out the date format is not a valid one to be passed to the Date()
function as a dateString
, so it need to be changed to a valid one such as yyyy-mm-dd
function changeDateFormat(dt) {
var tdt = dt.split(" ")[0].split("/");
return tdt[2] + "-" + tdt[1] + "-" + tdt[0];
}
Then you can use:
var today = new Date(changeDateFormat(previousDate));
Upvotes: 1