mongodb中Date类型数据入库时以0时区存储问题

luoyjx · 2014-08-29 14:49 · 2812次阅读

    date = new Date();//入库时间
    var localTime = date.getTime();
    var localOffset=date.getTimezoneOffset()*60000; //获得当地时间偏移的毫秒数
    var utc = localTime + localOffset; //utc即GMT时间
    var offset =16; //以北京时间为例,东8区
    var beijing = utc + (3600000*offset);
    date = new Date(beijing);
    data.time = date;
    var receiveTime = new Date(
        parseInt(data.DataTime.substring(0,4)),//年
        parseInt(data.DataTime.substring(4,6)) - 1,//月,从零开始,需要减一
        parseInt(data.DataTime.substring(6,8)),//日
        parseInt(data.DataTime.substring(8,10)),//时
        parseInt(data.DataTime.substring(10,12)),//分
        parseInt(data.DataTime.substring(12,14))//秒
    );
    localTime = receiveTime.getTime();
    localOffset = receiveTime.getTimezoneOffset() * 60000;
    utc = localTime + localOffset;
    beijing = utc + (3600000*offset);
    data.DataTime = new Date(beijing);

将时区再加8个之后入库就正常了

收藏

暂无评论

登录后可以进行评论。没有账号?马上注册