/**************************************************
说明:利用 NSDateComponents 来拆分年月日时分秒 例子
用途:以后编写周易软件会用到
作者:飘云
日期:2014-07-07
***************************************************/
- (void)showDateTime
{
    NSDate *senddate = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit|NSSecondCalendarUnit|NSHourCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents *component = [cal components:unitFlags fromDate:senddate];
    NSInteger year = [component year];
    NSInteger month = [component month];
    NSInteger day = [component day];
    NSInteger hour = [component hour];
    NSInteger minute = [component minute];
    NSInteger second = [component second];
    NSString *dateStr = [NSString stringWithFormat:@"%.4d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, day, hour, minute, second];
    NSLog(@"%@", dateStr);
}

/**************************************************
 说明:传统获取当前日期时间 例子
 用途:以后编写周易软件会用到
 作者:飘云
 日期:2014-07-07
 ***************************************************/
- (void)showDateTime2
{
    // 方法一
    NSDate *date = [NSDate date];
    NSTimeInterval  sec = [date timeIntervalSinceNow];
    NSDate *currentDate = [[NSDate alloc]initWithTimeIntervalSinceNow:sec];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSString *str = [fmt stringFromDate:currentDate];
    NSLog(@"%@", str);

    // 方法二
    NSString *currentDateStr = [fmt stringFromDate:[NSDate date]];
    NSLog(@"%@", currentDateStr);
}

你可能感兴趣的文章

评论区

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。