LoginSignup
22
22

More than 5 years have passed since last update.

Objective-CとSwiftでのUNIX時間の変換

Last updated at Posted at 2014-06-22

UNIX時間とは

1970年1月1日午前0時0分0秒からの経過秒数で表す時刻表現。

UNIX時間 - Wikipedia
http://ja.wikipedia.org/wiki/UNIX時間

Objective-Cの場合

main.m
// UNIX時間をNSDate形式に変換
NSString *stringDate = @"1402918625";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:stringDate.doubleValue];

// NSDateFormatterを使用して任意のフォーマットの日本標準時間に変換
NSDateFormatter *outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
[outputFormat setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSLog(@"Result:%@",[outputFormat stringFromDate:date]);

Swiftの場合

main.swift
// UNIX時間をNSDate形式に変換
let stringDate = "1402918625"
let date = NSDate(timeIntervalSince1970:Double(stringDate)!)

// NSDateFormatterを使用して任意のフォーマットの日本標準時間に変換
let outputFormat = NSDateFormatter()
outputFormat.locale = NSLocale(localeIdentifier: "ja_JP")
outputFormat.dateFormat = "yyyy/MM/dd HH:mm:ss"
print("Result:\(outputFormat.stringFromDate(date))")
22
22
0

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
22
22