NSDate

作者: OMSlhk | 来源:发表于2016-08-04 16:50 被阅读0次

    //初始化格式化时间 并设置时区 时间和NSString之间的转化

    NSDateFormatter *formater = [[NSDateFormatter alloc]init];

    formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    [formater setTimeZone:[NSTimeZone timeZoneWithName:@"UTC+8"]];

    NSDate * date = [NSDate date];

    NSString *date1 = [formater stringFromDate:date];

    //增加时间

    NSTimeInterval secondes = 24 * 60 * 60;

    NSDate * now = [NSDate date];

    //模拟昨天 24 * 60 * 60

    NSDate * yesDay = [now addTimeInterval:-secondes];

    //比较两个日期是否相同

    BOOL isEqual = [now isEqualToDate:yesDay];

    //获得两个日期中比较早的一个

    NSDate * earler = [yesDay earlierDate:now];

    NSLog(@"%@",earler);

    NSDate * later = [yesDay laterDate:now];

    NSLog(@"later %@",later);

    //把字符串转换成日期

    NSString * str = @"2014/03/20";

    NSDateFormatter * f = [[NSDateFormatter alloc] init];

    [f setDateFormat:@"yyyy/MM/dd"];

    NSDate * date = [f dateFromString:str];

    NSLog(@"date %@",date);

    相关文章

      网友评论

          本文标题:NSDate

          本文链接:https://www.haomeiwen.com/subject/ddocsttx.html