1

I keep looking for steps in the Internet on how I can change a format of a certain date

$j("#activitycalendar").datepicker("setDate", $j.datepicker.parseDate("yy-mm-dd",$_GET.critval)); 

All I want to do is to set the date of a datepicker depending on what the $_GET.critval contains. And the $_GET.critval contains a date in this format

2011-12-05

as the jqueryui documents says

"The new date may be a Date object or a string in the current date format (e.g. '01/26/2009')" jQuery UI - Datepicker Demos and Documentation

so I wanted to change the format of my $_GET.critval so it can change the date of the datepicker. I tried the $j.datepicker.parseDate but it really doesn't work. the return value of that parsing will be like this

Mon Dec 05 2011 00:00:00 GMT+0800 (Taipei Standard Time)

Is there any solution so I can format the date and set the date of the datepicker? thanks

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • 1
    This question has been answered http://stackoverflow.com/questions/1328025/jquery-ui-datepicker-change-date-format – Sara Jan 26 '12 at 07:38

1 Answers1

3

Check out moment.js! It's a really powerful little library for working with Dates in JavaScript.

var today = moment(new Date());
today.format("YYYY-M-D");                  // "2012-4-11"
today.format("MMMM D, YYYY h:m A");        // outputs "April 11, 2012 2:40 PM"

// in one line...
moment().format("YYYY-M-D");               // "2012-4-11"
moment().format("MMMM D, YYYY h:m A");     // outputs "April 11, 2012 2:40 PM"

Another example...

var a = moment([2012, 2, 12, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
a.format("ddd, hA");                       // "Mon, 3PM"

Also, its worth mentioning to checkout date.js. I think the two libraries complement each other.

Hristo
  • 45,559
  • 65
  • 163
  • 230