[英]Any Javascript DateTime Function return MySQL datetime format?
我有一個函數將ajax請求發送到帶參數的服務器文件。 問題是我想發送當前的DateTime值來比較它與數據庫。
作為MySQL數據時間格式,所有日期時間都顯示為“2012-02-03 19:50:28”格式。
我怎樣才能在Javascript中生成它。
另一個問題:我可以為當前日期時間添加小時數(以修復服務器時區問題)
提前致謝
嘗試這個
/* 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:
日期格式
javascript中的日期類型
使用日期
嘗試這個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.