最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Any Javascript DateTime Function return MySQL datetime format? - Stack Overflow

programmeradmin3浏览0评论

I've a function that send ajax request to a server file with parameters. Problem is i want to send current DateTime value to pare it from database.

as MySQL datatime format all datetimes are shown as "2012-02-03 19:50:28" format.

How can i generate it in Javascript.

Another question: Can i add hours to current datetime (to fix server time zone problem)

Thanks in advance

I've a function that send ajax request to a server file with parameters. Problem is i want to send current DateTime value to pare it from database.

as MySQL datatime format all datetimes are shown as "2012-02-03 19:50:28" format.

How can i generate it in Javascript.

Another question: Can i add hours to current datetime (to fix server time zone problem)

Thanks in advance

Share Improve this question asked Feb 4, 2012 at 6:09 Erhan H.Erhan H. 5204 gold badges14 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

try this

/* use a function for the exact format desired... */
function ISODateString(d){
  function pad(n){return n<10 ? '0'+n : n}
  return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate()) +' '
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())
}

var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28 19:03:12

Refernece:
date format
date type in javascript
working with dates

Try this:

Date.prototype.toMysqlFormat = function () {
    function pad(n) { return n < 10 ? '0' + n : n }
    return this.getFullYear() + "-" + pad(1 + this.getMonth()) + "-" + pad(this.getDate()) + " " + pad(this.getHours()) + ":" + pad(this.getMinutes()) + ":" + pad(this.getSeconds());
};
var TimeNow = new Date().toMysqlFormat();
alert(TimeNow); //Alerts Current TimeStamp

Parse date to your format string, you can do it by the Date API or use Datejs (a powerful date plugin).

But I remend you push millisecond number to server/mysql instead of string :

new Date().getTime();
发布评论

评论列表(0)

  1. 暂无评论