objective-c 获取特定时间的日期

来源:互联网 发布:怎样开通淘宝 编辑:程序博客网 时间:2024/05/17 14:25

1.根据本地时区输出设定日期:


NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  [dateFormatter setDateFormat:@"EEEE"];NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

2.根据本地时区把特定日期转换为周日数:


NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];int weekday = [comps weekday];


3.通用函数

-(void)getdate{NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];[dateFormat setDateFormat:@"yyyy-MM-dd"];NSDateFormatter *format = [[NSDateFormatter alloc] init];[format setDateFormat:@"MMM dd, yyyy HH:mm"];NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];[timeFormat setDateFormat:@"HH:mm:ss"];NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;[dateFormatter setDateFormat:@"EEEE"];NSDate *now = [[NSDate alloc] init];NSString *dateString = [format stringFromDate:now];NSString *theDate = [dateFormat stringFromDate:now];NSString *theTime = [timeFormat stringFromDate:now];NSString *week = [dateFormatter stringFromDate:now];NSLog(@"\n"      "theDate: |%@| \n"      "theTime: |%@| \n"      "Now: |%@| \n"      "Week: |%@| \n"      , theDate, theTime,dateString,week);}


0 0
原创粉丝点击