所以我使用 MySQL 作為我的節點應用程式的資料庫我可以插入YYYY-MM-DD格式的日期但是當我從中獲取資料時回傳它yyyy-mm-ddT00:00:00.000Z所以我只想要第一部分
db.query(`SELECT agereement_date FROM mt_A1 WHERE ledger_num = 15`,(err,data)=>{
console.log(data)
})
輸出是這樣的
[
RowDataPacket {
agereement_date: 2021-03-07T18:30:00.000Z,
}
]
我只想要YYYY-MM-DD第一部分我正在使用一些 JavaScript 來糾正它,但感覺沒有必要直接從 MySQL 獲取該格式的日期
uj5u.com熱心網友回復:
MySQL 資料庫中的時間沒有定義時區。
Date當您從服務器獲取時間時,NodeJS 會將時間轉換為物件。
因此,根據您的用例,有 3 種解決方案:
如果您不關心瀏覽器的時區:您可以添加
DATE_FORMAT到 sql 查詢并僅使用日期部分。它仍然不知道時區,但是您可以從后端的 db 獲取日期并使用以下方法剪切日期部分:
db.query('SELECT agereement_date FROM mt_A1 WHERE ledger_num = 15', (err,data) => {
data = data.map(row => {
row.agereement_date = row.agereement_date.toISOString().split('T')[0];
return row;
});
console.log(data);
})
- 如果要顯示瀏覽器時區的正確時間,則必須在前端級別使用該日期物件:
function strftime(sFormat, date) {
if (!(date instanceof Date)) date = new Date();
var nDay = date.getDay(),
nDate = date.getDate(),
nMonth = date.getMonth(),
nYear = date.getFullYear(),
nHour = date.getHours(),
aDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
aMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
aDayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
isLeapYear = function() {
if ((nYear&3)!==0) return false;
return nYear%100!==0 || nYear%400===0;
},
getThursday = function() {
var target = new Date(date);
target.setDate(nDate - ((nDay 6)%7) 3);
return target;
},
zeroPad = function(nNum, nPad) {
return ('' (Math.pow(10, nPad) nNum)).slice(1);
};
return sFormat.replace(/%[a-z]/gi, function(sMatch) {
return {
'%a': aDays[nDay].slice(0,3),
'%A': aDays[nDay],
'%b': aMonths[nMonth].slice(0,3),
'%B': aMonths[nMonth],
'%c': date.toUTCString(),
'%C': Math.floor(nYear/100),
'%d': zeroPad(nDate, 2),
'%e': nDate,
'%F': date.toISOString().slice(0,10),
'%G': getThursday().getFullYear(),
'%g': ('' getThursday().getFullYear()).slice(2),
'%H': zeroPad(nHour, 2),
'%I': zeroPad((nHour 11)%12 1, 2),
'%j': zeroPad(aDayCount[nMonth] nDate ((nMonth>1 && isLeapYear()) ? 1 : 0), 3),
'%k': '' nHour,
'%l': (nHour 11)%12 1,
'%m': zeroPad(nMonth 1, 2),
'%M': zeroPad(date.getMinutes(), 2),
'%p': (nHour<12) ? 'AM' : 'PM',
'%P': (nHour<12) ? 'am' : 'pm',
'%s': Math.round(date.getTime()/1000),
'%S': zeroPad(date.getSeconds(), 2),
'%u': nDay || 7,
'%V': (function() {
var target = getThursday(),
n1stThu = target.valueOf();
target.setMonth(0, 1);
var nJan1 = target.getDay();
if (nJan1!==4) target.setMonth(0, 1 ((4-nJan1) 7)%7);
return zeroPad(1 Math.ceil((n1stThu-target)/604800000), 2);
})(),
'%w': '' nDay,
'%x': date.toLocaleDateString(),
'%X': date.toLocaleTimeString(),
'%y': ('' nYear).slice(2),
'%Y': nYear,
'%z': date.toTimeString().replace(/. GMT([ -]\d ). /, '$1'),
'%Z': date.toTimeString().replace(/. \((. ?)\)$/, '$1')
}[sMatch] || sMatch;
});
}
const time = new Date('2021-03-08T00:30:00.000 02:00');
const localTime = strftime('%Y-%m-%d %H:%M:%S', time);
const localDate = strftime('%Y-%m-%d', time);
console.log(localTime)
console.log(localDate)
如您所見,我已以 ISO 格式定義, 02:00因此它可能會根據本地計算機的時間而不同地回傳。
uj5u.com熱心網友回復:
修改您的查詢:
SELECT DATE(agereement_date)
FROM mt_A1
WHERE ledger_num = 15
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522203.html
上一篇:Excel中兩個日期相減
下一篇:將分鐘和小時添加到字串時刻js
