14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift / PHP / JavaScript で UNIXタイムスタンプ を取得したい

Last updated at Posted at 2015-09-24

PHP 経験者が Swift 初経験で困ったことシリーズ。

秒までの取得

Swift

マイクロ秒数(Double 型)で取得できるので、整数型に変換。

let time:Int = Int(NSDate().timeIntervalSince1970)

PHP

$time = time();

JavaScript

ミリ秒で取得できるので、整数に変換。

var time = parseInt(Date.now() / 1000, 10);

// わかる人向け
var time = Date.now() / 1000 | 0;

ちなみに Date.now() はIE8以下では使えないので、考慮するなら。

var time = parseInt(new Date().getTime() / 1000, 10);

// わかる人向け
var time = new Date() / 1000 | 0;
14
11
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?