Reputation: 3920
I am trying to change the data format below to another form.
here is the date
I'm getting
var datetime =Thu Oct 03 2013 13:41:06 GMT+0530 (IST)
How can i get date
in this format 03-10-2013 13:41 PM
Upvotes: 0
Views: 1252
Reputation: 56509
If you want to do it in pure JS, then you have to do something like
var ms = new Date("Thu Oct 03 2013 13:41:06 GMT+0530 (IST)");
var curr_date = (ms.getDate()< 10) ? "0" + ms.getDate() : ms.getDate();
var curr_month = (ms.getMonth()< 9) ? "0" + (ms.getMonth()+1) : ms.getMonth()+1;
var curr_year = ms.getFullYear();
var hours = ms.getHours();
var min = ms.getMinutes();
suf = (hours >= 12)? 'pm' : 'am';
hours = hours % 12;
alert(curr_date + "-" + curr_month + "-" + curr_year + " " + hours + ":" + min + " " + suf);
To be simple, you can try using the following library
moment().format('DD-MM-YYYY, h:mm a');
Upvotes: 1
Reputation: 3669
Why not write a short function that splits the date string up. Then you can stick them back together in whatever order you like.
Here's a quick example:
function tidyDate(theDate) {
// split up the string into parts separated by colons or whitespace
var parts = theDate.toString().split(/[:\s]+/);
// Get the number of the month - Don't forget that it's zero-indexed
var month = theDate.getMonth() + 1;
// Let's say that it's morning
var AMPM = " AM";
// But we should check whether it's after noon
if (parseInt(parts[4]) >= 12){
AMPM = " PM";
}
return parts[2] + "-" + month + "-" + parts[3] + " " + parts[4] + ":" + parts[5] + AMPM;
}
tidyDate(new Date());
returns:
03-10-2013 13:41 PM
Upvotes: 2
Reputation: 1485
Posted that would help ur cause exactly..http://jsbin.com/OCikUZO/1/edit
function convertUTCDateToLocalDate(date) {
alert('hi');
var newDate = new Date(date.getTime());
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
var datetime = new Date("January 02, 2012 22:00:00 GMT+0530");
var date = convertUTCDateToLocalDate(new Date(datetime));
var now = date.toLocaleString();
Upvotes: 0
Reputation: 4845
Try this code
function dateFormat()
{
var d = new Date();
date = d.getDate();
date = date < 10 ? "0"+date : date;
mon = d.getMonth()+1;
mon = mon < 10 ? "0"+mon : mon;
year = d.getFullYear()
return (date+"/"+mon+"/"+year);
}
and let me know..
Upvotes: 0
Reputation: 337560
The format you see there is JS's verbose version used when a date is converted to a string. If you would like to specify a format yourself you would need to break down the parts of the date and concatenate them together using the various date methods, eg date.getYear() + '/' + date.getMonth()
etc.
Alternatively, you can use a library such as date.js which will do this for you, eg:
myDate.toString("dd-mm-yyyy")
Further reading on toString in date.js
Upvotes: 1