日期常用方法

常用方法

日期

  1. 獲取當(dāng)前日期
    [NSDate date]
  2. 獲取當(dāng)前日期開始,若干秒后的日期
    [NSDate dateWithTimeIntervalSinceNow:3600.0*10]
  3. 某個(gè)日期開始,若干秒后的日期
    [date1 dateByAddingTimeInterval:60]
  4. 從1970年開始,經(jīng)過了若干秒的時(shí)間
    [NSDate dateWithTimeIntervalSince1970:1000]

間隔

  1. 從1970年到某一時(shí)間的秒數(shù)
    [date timeIntervalSince1970]
  2. 計(jì)算二個(gè)日期的間隔
    [date1 timeIntervalSinceDate:date]
  3. 某個(gè)日期到現(xiàn)在的間隔
    [date1 timeIntervalSinceNow]

擴(kuò)展方法

  1. 時(shí)間戳轉(zhuǎn)指定格式的日期
    <pre> + (NSString *)secondTransYearMonther:(NSString *)time isAddHour:(BOOL)isAdd
    {
    NSTimeInterval _interval = [time doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    if (isAdd) {
    [objDateformat setDateFormat:@"yyyy年MM月dd日 hh:mm:ss"];
    }
    else{
    [objDateformat setDateFormat:@"yyyy年MM月dd日"];
    }
    NSString *temp = [objDateformat stringFromDate:date];
    return temp;
    }
    </pre>
  2. 字符串和日期互相轉(zhuǎn)換
    <pre>
    此時(shí)需要稍加注意formater格式要與字符串格式完全一致,否則轉(zhuǎn)換失敗。
    </pre>
  3. 星期幾的獲取
    <pre>+ (NSString *)weekdayStringFromDate:(NSDate*)inputDate
    {
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
    }
    </pre>
    <pre>
    根據(jù)項(xiàng)目的需要內(nèi)容會持續(xù)擴(kuò)充
    </pre>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容