关于查询postgres数据库返回时区问题:
egg.js 服务查询时间类型数据带时区(TZ)问题。 测试发现用sequelize从数据库获取到的数据的时间类型是正确的,然后其打印出来时间格式带时区(是js Date对象的toJSON)
目前在程序入口重写Date对象的toJSON方法,在toJSON方法中格式化时间类型,可解决服务返回日期类型带时区的问题。
function formatDate(date) {
const myyear = date.getFullYear();// 年
let mymonth = date.getMonth() + 1;// 月
let myweekday = date.getDate();// 日
let myhour = date.getHours();// 时
let myminutes = date.getMinutes();// 分
let myseconds = date.getSeconds();// 秒
if (mymonth < 10) {
mymonth = '0' + mymonth;
}
if (myweekday < 10) {
myweekday = '0' + myweekday;
}
if (myhour < 10) {
myhour = '0' + myhour;
}
if (myminutes < 10) {
myminutes = '0' + myminutes;
}
if (myseconds < 10) {
myseconds = '0' + myseconds;
}
return myyear + '-' + mymonth + '-' + myweekday + ' ' + myhour + ':' + myminutes + ':' + myseconds;
}
Date.prototype.toJSON = function () { return formatDate(this)}