简体   繁体   English

如何在JavaScript中将MSSQL日期时间整数转换为YYYY / mm / DD格式

[英]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.

相关问题 如何将 JavaScript 文件中的“yyyy-MM-dd”格式转换为 HTML 文件中的“dd/MM/yyyy”和“MM/dd/yyyy”格式? - How to convert “yyyy-MM-dd” format present in JavaScript file to “dd/MM/yyyy” and “MM/dd/yyyy” format in HTML file? Javascript:将datetime转换为DD / MM / YYYY - 时间? - Javascript: convert datetime to DD/MM/YYYY - Time? 如何使用JavaScript将ISOString转换为日期格式dd / mm / yyyy - How to convert ISOString to date format dd/mm/yyyy using javascript 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript? 如何将 JavaScript 日期格式化为 mm/dd/yyyy? - How to format JavaScript date to mm/dd/yyyy? 如何验证 MM/dd/yyyy hh:mm 格式的日期时间? - How to validate the DateTime of MM/dd/yyyy hh:mm format? 如何检查日期时间是否在范围之间? jQuery或javascript中的YYYY-MM-DD HH:MM:SS格式 - How to check datetime if it falls between a range? YYYY-MM-DD HH:MM:SS format in jquery or javascript 将javascript日期格式从yyyy.mm.dd转换为dd.mm.yyyy - Convert javascript date format from yyyy.mm.dd to dd.mm.yyyy 在JavaScript中将日期格式从mm-dd-yyyy转换为yyyy-mm-dd - Convert Date format from mm-dd-yyyy to yyyy-mm-dd in javascript 使用 Javascript 将 DD-MM-YYYY 转换为 YYYY-MM-DD 格式 - Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM