[英]How to convert MSSQL datetime integer to YYYY/mm/DD format in JavaScript
I get data from legacy API where there is a field 'opendate': 1086652416 I know that this is 24/06/2014 我从旧有字段“ opendate”的API中获取数据:1086652416我知道这是2014年6月24日
initially I have decide that this is unix_timestamp which should be multiply with 1000 and BINGO 最初我决定这是unix_timestamp,应与1000和BINGO相乘
Convert a Unix timestamp to time in JavaScript 将Unix时间戳转换为JavaScript中的时间
var unix_timestamp = 1086652416;
var date = new Date(unix_timestamp*1000);
console.log(date); // Date {Tue Jun 08 2004 06:53:36 GMT+0700 (NOVST)}
// As you see this is not 24/06/2014 !!!
99% that this is datetime format from MSSQL and it should be 24/06/2014 99%这是MSSQL的日期时间格式,应该是2014年6月24日
from early results: 1066467328 is 2011-12-15, 1066598400 is 2011-12-16 从早期结果来看:1066467328是2011-12-15,1066598400是2011-12-16
the question is: how could I convert this datetime to format YYYY-mm-dd in NodeJS 问题是:如何在NodeJS中将此日期时间转换为YYYY-mm-dd格式
From MSSQL documenation: 从MSSQL文档中:
Values with the datetime data type are stored internally by Microsoft SQL Server as two 4-byte integers. Microsoft SQL Server在内部将日期时间数据类型的值存储为两个4字节整数。 The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system reference date.
前4个字节存储基本日期(1900年1月1日)之前或之后的天数。基本日期是系统参考日期。 Values for datetime earlier than January 1, 1753, are not permitted.
不允许使用日期时间早于1753年1月1日的值。
Just mocked this method in firebug, should demonstrate a simple example to format a js date. 刚刚在firebug中嘲笑了此方法,应该演示一个简单的示例来格式化js日期。
function formatDate(date){
var df = "yyyy-MM-dd",
tmp = date.getDate();
df = df.replace("dd", tmp < 10 ? "0"+tmp : tmp);
tmp = date.getMonth()+1; //Add 1 for humans
df = df.replace("MM", tmp < 10 ? "0"+tmp : tmp);
df = df.replace("yyyy", date.getFullYear());
return df;
}
alert(formatDate(new Date()));
You are on the right track, you are correctly converting the unix timestamp to a javascript date. 您处在正确的轨道上,您正在正确地将unix时间戳转换为javascript日期。
Where you are wrong is that unix timestamp 1086652416 is NOT 24/06/2014. 您输入的错误是Unix时间戳1086652416不是2014年6月24日。 It is Mon, 07 Jun 2004 23:53:36 GMT
现在是格林尼治标准时间2004年6月7日星期一
You should try this: 您应该尝试这样:
var date = new Date(1086652416 * 1000);
var commonTime = date.toLocaleString();
console.log(commonTime);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.