Difference in UTC date between PHP and Javascript

工作中,因使用highcharts显示数据,需要将PHP 将日期转换为UTC 时区的时间截,然后通过json返回。而在网页中,显示时需要将UTC 时间截装换为本地时间。

google:javascript date utc to local 一番之后,找到

  1. UTC date and time to local
  2. Date.prototype.getTimezoneOffset()

知道找到了UTC的时间截 date.toString()

 //2016-02-29 15:18:20
var utcDate = new Date(1456759100000);
console.log(utcDate.toString());

结果是:

Mon Feb 29 2016 23:18:20 GMT+0800

慢了8个钟

故修改成:

 //2016-02-29 15:18:20
var utcDate = new Date(1456759100000);
console.log(utcDate.toString());
var today = new Date();
var currentTimeZoneOffsetInHours = today.getTimezoneOffset() / 60;
//console.log(currentTimeZoneOffsetInHours);
var datetimes = (utcDate.getHours() + currentTimeZoneOffsetInHours) +":"+utcDate.getMinutes()+":"+utcDate.getSeconds();
console.log(datetimes);

运行结果:

因显示主要是小时慢了8个小时,所以只计算了小时部分。

js Date.UTC() 与 php strtotime()生成的时间截不一样的更多相关文章

  1. js Date对象要注意的问题(时间转换)

    1.时间戳和时间对象可以灵活转变: let n = new Date() // 返回的是当前时间对应的国际时间 let nt =n.getTime() let n2 =new Date(nt) con ...

  2. js中:Date.utc()方法与getTime()方法返回值不相等的原因

    // Date.UTC() 方法接受的参数同日期构造函数接受最多参数时一样,返回从1970-1-1 00:00:00 UTC到指定日期的的毫秒数. var utcDate = Date.UTC(201 ...

  3. JS中Date.parse()和Date.UTC()返回值不一致

    Date.parse() 方法解析一个表示某个日期的字符串,并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包 ...

  4. 1 时间戳 2 C# 如何生成一个时间戳 3 js 时间加一分钟... 4 js string->date 5 js 取得当天0点 / 23:59:59 时间

    var str = 'Jan 23, 2019 10:25:47 AM';var strnow = new Date(str); 时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经 ...

  5. JS Date当前时间:获取日期时间方法在各浏览器中的差异

    转自:http://www.feiesoft.com/00047/<script type="text/javascript"> // JS Date当前时间获取方法在 ...

  6. js生成当前时间

    js生成当前时间 var today=new Date(); function itArray(){ this.length=itArray.arguments.length for(var i=0; ...

  7. 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!

    转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11     ...

  8. Js Date泣血整理

    原文:Js Date泣血整理 JS Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. ...

  9. js date 和 math

    Math 用于执行常用的数学任务 console.log(Math.E); 自然数底数2.718 console.log(Math.PI); 圆周率3.1415926 console.log(Math ...

随机推荐

  1. Algorithm: quick sort implemented in python 算法导论 快速排序

    import random def partition(A, lo, hi): pivot_index = random.randint(lo, hi) pivot = A[pivot_index] ...

  2. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...

  3. DP——最优三角形剖分

    [动态规划]凸多边形最优三角剖分 枚举三角行,再递归三角形旁边的两个多边形.

  4. 基于adt-bundle的Android开发环境搭建

    web与移动是当今的热门,怎么说都得会一点,完全不懂是不行的. 一直想玩一下移动开发,穷屌丝暂时没有iPhone和Mac,所以先拿Android开刀. 之前也有想过玩一下Android,但是都被各种博 ...

  5. HW6.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. Java之文件的随机访问和读写RandomAccessFile

    package FileDemo; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAc ...

  7. 把存储过程获取的数据输出到报表的html模板中

    制作报表的html模板 <HTML><meta http-equiv="Content-Type" content="text/html; charse ...

  8. Head First设计模式-观察者模式

    一.整体代码 Subject.java public interface Subject { public void registerObserver(Observer o); public void ...

  9. rxjs5.X系列 —— Combination/Multicasting系列 api 笔记

    欢迎指导与讨论 :) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第三篇 -- Combination组合与Multicasting广播.如有错漏,希望大家指出提醒O ...

  10. jQuery的遍历方法

    1.jQuery中的map使用方法 <!DOCTYPE html> <html> <head> <style>p { color:red; }</ ...