博客
关于我
时间:UTC时间、GMT时间、本地时间、Unix时间戳
阅读量:463 次
发布时间:2019-03-06

本文共 1100 字,大约阅读时间需要 3 分钟。

UTC时间与本地时间的转换及Unix时间戳的应用

在计算机领域,UTC(协调世界时)是全球通用的标准时间,常用于国际电报和通信系统。UTC与本地时区之间的关系是通过时区差来确定的。

UTC与本地时的关系

UTC与本地时间的关系可以通过时区差来表示。例如,东八区(如北京)时区差为+0800。根据公式:

UTC = 本地时间 - 时区差

因此,UTC = 本地时间(北京时间) - 0800。

Unix时间戳的计算

Unix时间戳是从1970年1月1日00:00:00起至现在所经过的秒数。这个时间点是计算机内部常用的时间标准。在不同的编程语言中获取当前时间戳的方式如下:

Java

System.currentTimeMillis() / 1000

JavaScript

Math.round(new Date().getTime() / 1000)

C#

(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000

MySQL

SELECT unix_timestamp(now())

Python

import timetime.time()

Unix/Linux

date +%s

时间戳与普通时间的转换

将Unix时间戳转换为普通日期时间的方法在不同编程语言中也有所不同:

Java

String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp * 1000));

JavaScript

var commonTime = new Date(Date.UTC(year, month - 1, day, hour, minute, second));

C#

var date = new DateTime(1970,1,1).AddSeconds(Unix timestamp);

MySQL

SELECT from_unixtime(Unix timestamp)

Python

import timeint(time.mktime(time.strptime('YYYY-MM-DD HH:MM:SS', '%Y-%m-%d %H:%M:%S')))

SQL Server

SELECT DATEDIFF(s, '1970-01-01 00:00:00', time)

Unix/Linux

date -d "Jan 1, 1970 00:00:01"

转载地址:http://dkiyz.baihongyu.com/

你可能感兴趣的文章
npm前端包管理工具简介---npm工作笔记001
查看>>
npm包管理深度探索:从基础到进阶全面教程!
查看>>
npm升级以及使用淘宝npm镜像
查看>>
npm发布包--所遇到的问题
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和package.json那些不为常人所知的小秘密
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm如何清空缓存并重新打包?
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>