为什么我的mysql中的日期在javascript中递减一天? [英] Why is my date from mysql decrementing by one day in javascript?

查看:110
本文介绍了为什么我的mysql中的日期在javascript中递减一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个待办事项列表,存储在mysql数据库中,存储的列是todoTitle和todoDate,当我将todoDate打印到屏幕上时(如您在下面的代码中看到的那样),它将显示日期减少了一天,例如,如果数据库中的日期显示为2016-12-20,它将在我的网站上显示为2016-12-19.

I have a todo list that is stored in mysql database the columns stored are todoTitle and todoDate, when i print the todoDate to screen as you can see in the code below it will show the date decremented by one day, for example if the date in the database shows 2016-12-20 it will show 2016-12-19 on my website.

如果您想知道为什么将todoDate制成字符串然后再子字符串,这是因为如果我不这样做,它会像这样打印出来:2016-12-19T23:00:00.000Z

If you are wondering why the todoDate is made into string and then substring it is because if i would not do that it would print out like this: 2016-12-19T23:00:00.000Z

var xhr = new XMLHttpRequest();                 // Create XMLHttpRequest object

xhr.onload = function() {// When readystate changes
// The following conditional check will not work locally - only on a server
if(xhr.status === 200) {                      // If server status was ok
responseObject = JSON.parse(xhr.responseText);

// BUILD UP STRING WITH NEW CONTENT (could also use DOM manipulation)
var newContent =
    "<tr>" +
    "<td>" + "Activity" + "</td>" +
    "<td>" + "Due Date" + "</td>" +
    "</tr>";
for (var i = 0; i < responseObject.length; i++) { // Loop through object
    newContent += "<tr>";
    newContent += "<td>" + responseObject[i].todoTitle + "</td>";
    newContent += "<td>" + responseObject[i].todoDate.toString().substring(0,10) + "</td>";
    newContent += "</tr>";
}

// Update the page with the new content
document.getElementById('content').innerHTML = newContent;

}
};

//xhr.open('GET', 'data/data.json', true);  // Dummy JSON Data
xhr.open('GET', 'http://127.0.0.1:8000/todo/', true);        // Prepare the request
xhr.send(null);                                 // Send the request

推荐答案

Z表示零时偏移",也称为祖鲁时间"(UTC).当您从数据库中查询日期时,在数据库层或应用程序层中都有两种可能的日期发生更改的情况,将其调整为您所在的时区.

The Z means "zero hour offset" also known as "Zulu time" (UTC). When you are querying the date from the database there are two possible scenario that the date is change, either in the database layer or on the application layer, adjusting it to the timezone you are at.

例如,如果数据库设置在获取实际数据时自动将时间保存到UTC,它将被转换为当前时区.但是从您的示例2016-12-20转换为2016-12-19T23:00:00.000Z时,我假设您的数据库设置在日期上将其保存在某个时区中,然后将其转换为UTC.

So for example if the database setting save time automatically to UTC when you got the actual data it will be converted to your current time zone. But from your example 2016-12-20 is converted to 2016-12-19T23:00:00.000Z then I am assuming that your database setting on date is saving it in a certain timezone then converting it to UTC.

要解决此问题,请尝试调整您的应用程序逻辑或数据库设置,对我而言,我宁愿在应用程序级别执行此操作,并在数据库中维护要保存到UTC的日期.

To fix it try adjusting your application logic or database setting, for me I rather do it on the application level and maintain the date in DB to be save to UTC.

尝试此操作以查看区别,并为您提供解决问题的提示:

Try this to see the difference and could give you hint on solving your issue:

var currentDate = new Date();
var isoDate = currentDate.toISOString();
console.log(currentDate, isoDate);

这篇关于为什么我的mysql中的日期在javascript中递减一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆