Posted By

jonniespratley on 07/24/09


Tagged

mysql timestamp


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thastyle


JavaScript - MySQL Timestamp


 / Published in: JavaScript
 

  1. /**
  2.  * I make a mysql date timestamp
  3.  * @deprecated - Datepicker used instead
  4.  * @param {Object} dateobj - a date
  5.  */
  6. function jps_makeTimestamp( dateobj )
  7. {
  8. var date = new Date( dateobj );
  9. var yyyy = date.getFullYear();
  10. var mm = date.getMonth() + 1;
  11. var dd = date.getDate();
  12. var hh = date.getHours();
  13. var min = date.getMinutes();
  14. var ss = date.getSeconds();
  15.  
  16. var mysqlDateTime = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;
  17.  
  18. return mysqlDateTime;
  19. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: craniumslows on April 30, 2011

I modified this a bit for my purposes. I know you advised of deprecation just a heads up for future visitors

//Thanks http://snipplr.com/view.php?codeview&id=17418 function makeTimestamp( ) { //modified to have param dateobj be optional and default be current date / time if (!arguments[0]) { var dateobj = new Date(); } else { var dateobj = arguments[0]; } var date = new Date( dateobj ); var yyyy = date.getFullYear(); var mm = date.getMonth() + 1; if (mm < 10 ) { mm = "0" + mm; } var dd = date.getDate(); if (dd < 10 ) { dd = "0" + dd; } var hh = date.getHours(); if (hh < 10 ) { hh = "0" + hh; } var min = date.getMinutes(); if (min < 10 ) { min = "0" + min; } var ss = date.getSeconds(); if (ss < 10 ) { ss = "0" + ss; }

var mysqlDateTime = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;

return mysqlDateTime;

}

You need to login to post a comment.