ios 将NSDate精确到天 (通用版本)

来源:互联网 发布:浪潮服务器数据恢复 编辑:程序博客网 时间:2024/06/08 03:52

        NSDate *nowDate1 = [NSDatedate];

        NSDateFormatter *formatter = [[[NSDateFormatteralloc]init] autorelease];

        [formatter setDateFormat:(@"yyyy-MM-dd")];

     NSMutableString *dateStr = [NSMutableStringstringWithString: [formatterstringFromDate:nowDate1]];//[NSMutableString stringWithFormat: @"%@ 00:00:00 +0000"];

        NSLog(@"%@", dateStr);

        

        nowDate1 = [self NSStringDateToNSDate: dateStr];

        NSLog(@"%@",nowDate1);

        - (NSDate *)NSStringDateToNSDate:(NSString *)string 

{    

    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

    [formatter setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];

    #define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd")

    [formatter setDateFormat:kDEFAULT_DATE_TIME_FORMAT];

    NSDate *date = [formatter dateFromString:string];

    [formatter release];

    return date;

}

原创粉丝点击