JavaScript, jQuery, MySQL

jQuery Plugin: MySQL DateTime to Input Date Format

Simple solutions to convert MySQL DateTime to Input Date Format. You may change the format as your requirement.

$.extend({
  getInputDateFromMySQLDate: function(value) {
  	var t = value.split(/[- :]/);

	var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
	
	var day = ("0" + d.getDate()).slice(-2);
	var month = ("0" + (d.getMonth() + 1)).slice(-2);

	return d.getFullYear()+"-"+(month)+"-"+(day);
  },
  getDateFromMySQLDate: function(value){
  	var t = value.split(/[- :]/);

	return new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
  }
});

References:

  1. http://stackoverflow.com/questions/3075577/convert-mysql-datetime-stamp-into-javascripts-date-format
  2. http://stackoverflow.com/questions/12346381/set-date-in-input-type-date

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + 6 =