1
votes

How to change the date format in Jquery/Javascript?

I've downloaded the date.js. Then, I follow below example:

var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));

So I write my own code for following date:

Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)

var d1 = Date.parse("Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)");
alert(d1.toString('dd/mm/yyyy'));

My logcat prints this:

CordovaLog(31455): Uncaught RangeError: toString() radix argument must be between 2 and 36

I use Phonegap for my android application.

2
Try using moment instead. It's really good for date parsing and formatting momentjs.com I see momentjs as the closest we have to JodaTime in javascript. Another option is to use the date formater of the jquery datepicker, but I think momentjs is better. - fmsf
I'm really interested with momentjs but the documentation is really long and I can't find any way to convert from timestamp to my formatted date. - Rendy
moment(TimeStampInMillis).format('DD/MM/YYYY') - fmsf

2 Answers

0
votes

try out this..

var d = new Date(); 
var newDate = d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear();

alert('newDate '+newDate);
-1
votes

You can use JavaScript to format date and time. http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

function formatDate(date) {
    var d1 = Date.parse(date);
    var d = new Date(d1);
    var curr_date = d.getDate();
    if (curr_date.toString().length == 1) {
        curr_date = "0" + curr_date;
    }
    var curr_month = d.getMonth();
    curr_month++;
    if (curr_month.toString().length == 1) {
        curr_month = "0" + curr_month;
    }
    var curr_year = d.getFullYear();
    console.log(curr_date + "/" + curr_month + "/" + curr_year);
}

formatDate("Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)");
//Returns the date in "dd/mm/yyyy" format