[英]SQL date discrepancy with some dates
I am working on a date problem. 我正在处理日期问题。
So, in the app I have a table that have some user records that includes a date of birth field (datetime sql type). 因此,在应用程序中,我有一个表,其中包含一些用户记录,其中包括出生日期字段(datetime sql类型)。 The problem is that for some users whose date of birth is prior to 1954, the date is not properly reflected.
问题是,对于出生日期在1954年之前的一些用户,日期没有得到适当反映。
For example, I have a user whose date of birth is 11/08/1920 but when I set the date of birth via server script, it ends up with the value 11/07/1920 23:23:24 . 例如,我有一个用户的出生日期是11/08/1920但是当我通过服务器脚本设置出生日期时,它最终得到值11/07/1920 23:23:24 。
I am getting the date value from a spreadsheet and the server script looks like this: 我从电子表格中获取日期值,服务器脚本如下所示:
function importDOBs(){
var allRecs = [];
var ss = SpreadsheetApp.openById("adfsasdfasdfasdfasdfasdf");
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2,4,sheet.getLastRow(),2).getValues();
for(var i=0; i<5; i++){
var row = data[i];
var date = row[0];
var oldMrn = row[1];
var query = app.models.purgedRecords.newQuery();
query.filters.oldMrn._equals = oldMrn;
var record = query.run()[0];
if(record){
var dob = new Date(date);
record.dateOfBirth = dob;
allRecs.push(record);
}
}
app.saveRecords(allRecs);
}
These are the values in the spreadsheet (they are strings, not dates): 这些是电子表格中的值(它们是字符串,而不是日期):
1954-03-04T00:00:00
2014-03-01T00:00:00
1951-10-20T00:00:00
1920-11-08T00:00:00
1938-09-27T00:00:00
However, somehow I'm always getting this: 但是,不知怎的,我总是得到这个:
As you see, there is a discrepancy on the dates that are prior to 1954. So far, I have tried other things such as changing this part: 如你所见,1954年之前的日期存在差异。到目前为止,我已尝试过其他一些事情,例如更改此部分:
if(record){
var dob = new Date(date);
record.dateOfBirth = dob;
allRecs.push(record);
}
to this: 对此:
if(record){
var dob = Utilities.formatDate(new Date(date),"GMT","yyyy-MM-dd'T'HH:mm:ss'Z'");
var birthDate = new Date(dob);
record.dateOfBirth = birthDate;
allRecs.push(record);
}
and the above resulted in the same thing. 而上述结果也是如此。 I have tried other dates after 1954 and they also seem wrong.
我在1954年之后尝试了其他日期,他们似乎也错了。 For example 05/19/1968 reflects 05/18/1968 23:00:00 .
例如, 05/19/1968反映了05/18/1968 23:00:00 。 So my best guess so far this has to do something with the daylight savings, perhaps?
所以到目前为止我最好的猜测是,这可能与日光节约有关吗?
The snake pit of converting dates between platforms
在平台之间转换日期的蛇坑
Try to set the values to UTC or just format them into the datetime
format for MySQL 尝试将值设置为UTC或只是将它们格式化为MySQL的
datetime
格式
The first option as seen in the snippet below requires you to convert the format in SQL: 下面的代码段中显示的第一个选项要求您在SQL中转换格式:
See this answer: MySQL yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss 看到这个答案: MySQL yyyy-mm-ddThh:mm:ss.sssZ到yyyy-mm-dd hh:mm:ss
DATE_FORMAT(STR_TO_DATE(«string»,'%Y-%m-%dT%H:%i:%s.000Z'),'%Y-%m-%d %H:%i:%s');
//Using Z to set to UTC let allRecs = []; document.querySelector("pre").textContent.split("\\n").forEach(function(element) { if (element != "") { const dob = new Date(element + "Z"); //Z forces te date to be in UTC with zero time offzet or: 00:00:00 const dateOfBirth = dob; allRecs.push(dateOfBirth); } }); console.log(allRecs); allRecs = []; //format to MySQL datetime format document.querySelector("pre").textContent.split("\\n").forEach(function(element) { if (element != "") { element = element.replace("T", " "); //replace T with space //now the string is in the datetime format for MySQL allRecs.push(element); } }); console.log(allRecs);
<pre> 1954-03-04T00:00:00 2014-03-01T00:00:00 1951-10-20T00:00:00 1920-11-08T00:00:00 1938-09-27T00:00:00 </pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.