What would be the best way to convert a mysql date format date into a javascript Date object?
將mysql日期格式的日期轉換為javascript日期對象的最佳方式是什么?
mySQL date format is 'YYYY-MM-DD' (ISO Format).
mySQL日期格式是“YYYY-MM-DD”(ISO格式)。
9 个解决方案
#1
38
Given your clarification that you cannot change the format of the incoming date, you need something like this:
考慮到你的澄清,你不能更改傳入日期的格式,你需要這樣的東西:
var dateParts = isoFormatDateString.split("-");
var jsDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2].substr(0,2));
Original response:
最初的反應:
Is there a reason you can't get a timestamp instead of the date string? This would be done by something like:
是否有一個原因導致您無法獲得時間戳而不是日期字符串?可以這樣做:
SELECT UNIX_TIMESTAMP(date) AS epoch_time FROM table;
Then get the epoch_time into JavaScript, and it's a simple matter of:
然后將epoch_time放到JavaScript中,很簡單:
var myDate = new Date(epoch_time * 1000);
The multiplying by 1000 is because JavaScript takes milliseconds, and UNIX_TIMESTAMP gives seconds.
乘以1000是因為JavaScript需要毫秒,而UNIX_TIMESTAMP需要秒。
#2
24
The shortest and fast method:
最短快速的方法:
var mySQLDate = '2015-04-29 10:29:08';
new Date(Date.parse(mySQLDate.replace('-','/','g')));
#3
2
UNIX timestamp (milliseconds since the January 1, 1970) would be my preferred choice.
UNIX時間戳(自1970年1月1日以來的毫秒)將是我的首選。
You can pass it around as an integer and use the JS setTime() method to make a JS Date object from it.
您可以將它作為一個整數傳遞,並使用JS setTime()方法從中創建一個JS日期對象。
#4
2
In AngularJS Way, Thanks to @Jonas Sciangula Street
感謝@Jonas Sciangula街
.filter('toJSDate', function(){
return function (dateString) {
return new Date(Date.parse(dateString.replace('-','/','g')))
};
})
#5
1
Stumbled on this looking to do exactly what the OP wanted, and used jhurshman answer so I +1'ed it, but it is incomplete as it will use 00:00:00 GMT-0500 (CDT) for the time part as it is not passed to the date object (var jsDate).
無意中發現了這一點,並按照OP的要求執行,並使用了jhurshman的答案,因此我對它進行了+1'ed,但它是不完整的,因為它將在時間部分使用00:00:00 GMT-0500 (CDT),因為它沒有傳遞到日期對象(var jsDate)。
In case anyone else is looking to create the full Javascript date object
如果其他人希望創建完整的Javascript日期對象
ex:
例:
Sat Mar 29 2014 23:24:28 GMT-0500 (CDT)
Based on a MySQL DATETIME format
基於MySQL DATETIME格式。
ex:
例:
'9999-12-31 23:59:59'.
You will want to use the following:
您將希望使用以下內容:
var dateParts = isoFormatDateString.split("-");
var jsDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2].substr(0, 2), dateParts[2].substr(3, 2), dateParts[2].substr(6, 2), dateParts[2].substr(9, 2));
It will return in the browsers current timezone as you are creating the date object, but I would do the timezone conversion on the backend in most cases.
當您創建日期對象時,它將返回到瀏覽器當前時區,但在大多數情況下,我將在后端執行時區轉換。
#6
1
mySqlDate is in the format "yyyy-mm-dd".
mySqlDate的格式是“yyy-mm-dd”。
var javaDate = new Date(mySqlDate);
This line of code works fine for me.
這行代碼對我來說很有用。
#7
0
You can split the date '-' as separator and use this constructor:
您可以分割日期'-'作為分隔符,並使用此構造函數:
var d = new Date(Y, M - 1, D);
#8
0
Building onto Jhurisman's answer. If you would want to set the date's time as well use the following:
建築到Jhurisman的答案。如果您想設置日期的時間,也可以使用以下方法:
var dateParts = mysqlDate;
var timeParts = dateParts[2].substr(3).split(":");
var jsDate = new Date(
dateParts[0],
dateParts[1] - 1,
dateParts[2].substr(0,2),
timeParts[0],
timeParts[1],
timeParts[2]
);
#9
0
While JS does possess enough basic tools to do this, it's pretty simple to use
雖然JS擁有足夠的基本工具來實現這一點,但是使用起來非常簡單
/**
* Ashu, You first need to create a formatting function to pad numbers to two digits…
**/
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
var date = new Date();
document.getElementById("date").innerHTML = date;
document.getElementById("mysql_date").innerHTML = date.toMysqlFormat();
console.log(date.toMysqlFormat());
Simple date:
Converted Mysql Fromat: