LoginSignup
3

More than 5 years have passed since last update.

日付に関する処理メモ(随時追加)

Last updated at Posted at 2014-06-27
DateUtility
/* 2つのNSDate型変数が同じ日付であるかのチェック */
// 同じ日付か?YES:同じ日付 NO:違う
// oneDate 比較対象の日付1
// otherDate 比較対象の日付2

+ (BOOL)isEqualDate:(NSDate *)oneDate otherDate:(NSDate *)otherDate{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy/MM/dd";
    NSString *oneDateStr = [dateFormatter stringFromDate:oneDate];
    NSString *otherDateStr = [dateFormatter stringFromDate:otherDate];

    return [oneDateStr isEqualToString:otherDateStr];
}

/* 現地時間の取得 */
+ (NSDate *)getLocaleDate{

    NSDate *tmpDate = [NSDate date];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger seconds = [timeZone secondsFromGMTForDate:tmpDate];
    NSDate *localDate = [tmpDate dateByAddingTimeInterval:seconds];

    return localDate;
}

/* Unix時間からのNSDateの取得 */
// Unix時間をミリ秒を変換する場合
+ (NSDate *)dateFromUnixTimeMilisecond:(unsigned long long)unixTime{
    NSInteger miliSeconds = 1000;
    unsigned long long unixTimeSecond = unixTime / miliSeconds;
    NSDate *dateFromUnixTime = [NSDate dateWithTimeIntervalSince1970:unixTimeSecond];

    return dateFromUnixTime;
}

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
3