各种编程语言获取当前时间戳的方法

 蓝色智慧 发表于 2021年03月23日 软件编程  65
蓝色智慧
LV 7 [大学]
最后在线:13天前
加入时间:2年前
主帖:233  跟帖:5

  时间戳是一种时间表示方式,定义为从格林威治时间1970年01月01日08时00分00秒起至现在的总秒数。


Swift:

NSDate().timeIntervalSince1970


Go
import (
      "time"
    )

    int32(time.Now().Unix())


Java
// pure java
    (int) (System.currentTimeMillis() / 1000)
// joda

    (int) (DateTime.now().getMillis() / 1000)


JavaScript:

Math.round(new Date() / 1000)


Objective-C:

[[NSDate date] timeIntervalSince1970]


MySQL:

SELECT unix_timestamp(now())


SQLite:

SELECT strftime('%s', 'now')


Erlang:

calendar:datetime_to_gregorian_seconds(calendar:universal_time())-719528*24*3600.


PHP
// pure php
    time()
// Carbon\Carbon

    Carbon::now()->timestamp


Python:
import time

time.time()


Ruby:

Time.now.to_i


Shell:

date +%s


Groovy“

(new Date().time / 1000).intValue()


Lua

os.time()


.NET/C#:

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


论坛主帖
234
论坛跟帖
2
在线访客
0
今日主帖
0
今日跟帖
0
今日注册
0
34
http://www.76w.net/sq/index.php/gentie.html
http://www.76w.net/sq/index.php/postzan.html
http://www.76w.net/sq/index.php/postcai.html
http://www.76w.net/sq/index.php/postshoucang.html
http://www.76w.net/sq/index.php/gentiezan.html
http://www.76w.net/sq/index.php/gentiecai.html
http://www.76w.net/sq/index.php/huifu.html
http://www.76w.net/sq/index.php/xiugai.html
http://www.76w.net/sq/index.php/shanchugentie.html
1
XzA=
1
© 2023 天慧网·互动社区 版权所有
Powered by JianYuLunTan
蒙ICP备2021000971号-1
http://www.76w.net/sq/index.php/feedback.html
http://www.76w.net/sq/index.php/qiandao.html
http://www.76w.net/sq/index.php/denglu.html
0
http://www.76w.net/sq/index.php/adenglu.html
剑鱼论坛