IOS 时间格式 时间转换 大总结
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
//实例化一个NSDateFormatter对象
p>
p>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
p>
p>//设定时间格式,这里可以设置成自己需要的格式
p>
p>[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
p>
p>//用[NSDate date]可以获取系统当前时间
p>
p>NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
p>
p>//输出格式为:2010-10-27 10:22:13
p>
p>NSLog(@"%@",currentDateStr);
p>
p>//alloc后对不使用的对象别忘了release
p>
p>[dateFormatter release];
p>
p>
p>
p>
p>///根据时间字符串获得当前星期几--
p>
p> -(void)GetTime
p>
p> {
p>
p> //根据字符串转换成一种时间格式 供下面解析
p>
p> NSString* string = @"2013-07-16 13:21";
p>
p> NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
p>
p> [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
p>
p> NSDate* inputDate = [inputFormatter dateFromString:string];
p>
p>
p> NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
p>
p> NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
p>
p> NSInteger unitFlags = NSYearCalendarUnit |
p>
p> NSMonthCalendarUnit |
p>
p> NSDayCalendarUnit |
p>
p> NSWeekdayCalendarUnit |
p>
p> NSHourCalendarUnit |
p>
p> NSMinuteCalendarUnit |
p>
p> NSSecondCalendarUnit;
p>
p>
p> comps = [calendar components:unitFlags fromDate:inputDate];
p>
p> int week = [comps weekday];
p>
p> NSString *strWeek = [self getweek:week];
p>
p> self.textfield.text = strWeek;
p>
p> NSLog(@"week is:%@",strWeek);
p>
p> }
p>
p> -(NSString*)getweek:(NSInteger)week
p>
p> {
p>
p> NSString*weekStr=nil;
p>
p> if(week==1)
p>
p> {
p>
p> weekStr=@"星期天";
p>
p> }else if(week==2){
相关新闻>>
- 发表评论
-
- 最新评论 更多>>