Gowri
Gowri

Reputation: 16845

Change given date format in js

I'm having date "1/19/12 00:00:00" (M/D/Y) .I want to change this date as "2012-01-19 00:00:00"(Y-M-D).

How can i do this.

I'm using jQuery ui date picker.Date picker formate is dateFormat:"m/d/y" I want to convert this date in on select.

onSelect: function( selectedDate ) {
//here i want to convert
}

Upvotes: 1

Views: 1225

Answers (4)

RobG
RobG

Reputation: 147483

If you just want to reformat the string:

var d = '1/19/12 00:00:00';

function convert(d) {
  var b = d.split(/[\/ ]/g);
  return '20' + b[2] + '-' + z(b[0]) + '-' + z(b[1]) + ' ' + b[3];

  function z(n) {
    n = Number(n);
    return (n<10? '0' : '') + n;
  }
}

alert( convert(d) ); // 2012-01-19 00:00:00

Upvotes: 0

Pencho Ilchev
Pencho Ilchev

Reputation: 3241

$('#datepicker').datepicker({
   onSelect: function(dateText, inst) { 
       var d = new Date(inst.selectedYear,inst.selectedMonth,inst.selectedDay);
       var mydate = $.datepicker.formatDate( 'yy/mm/dd', d);
       alert(mydate);
   }
});

a jsfiddle for this

Documentation for formatDate

Upvotes: 1

Ghostman
Ghostman

Reputation: 6114

      Date.format = 'yyyy-mm-dd';
    $(function()
    {
      $('.date-pick').datePicker()
     });

Try this

   $valz123=YOUR INPUT DATE

   var d =parseDate($valz123);

  $valz123=d.format("yyyy-mm-dd");


    function parseDate(input) 
{

var parts = input.match(/(\d+)/g);
return new Date(parts[0], parts[1]-1, parts[2]);

}

Upvotes: 0

ijse
ijse

Reputation: 3035

The dateFormat to be used for the altField option. This allows one date format to be shown to the user for selection purposes, while a different format is actually sent behind the scenes. For a full list of the possible formats see the formatDate function

Code examples

Initialize a datepicker with the altFormat option specified.

$( ".selector" ).datepicker({ altFormat: 'yy-mm-dd' });

Get or set the altFormat option, after init.

//getter
var altFormat = $( ".selector" ).datepicker( "option", "altFormat" );
//setter
$( ".selector" ).datepicker( "option", "altFormat", 'yy-mm-dd' );

Upvotes: 0

Related Questions