NSDate相关

获取当前时区的时间

[NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题

NSDate *date = [NSDate date];

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: date];

NSDate *localeDate = [date  dateByAddingTimeInterval: interval];  

NSLog(@"%@", localeDate);

 

获取date中的年月日时分秒  星期

-(NSString *)getDayWeek:(int)dayDelay{  

    NSString *weekDay;

    NSDate *dateNow;

    dateNow=[NSDate dateWithTimeIntervalSinceNow:dayDelay*24*60*60];//dayDelay代表向后推几天,如果是0则代表是今天,如果是1就代表向后推24小时,如果想向后推12小时,就可以改成dayDelay*12*60*60,让dayDelay=1   

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//设置成中国阳历     

    NSDateComponents *comps = [[NSDateComponents alloc] init];     

    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;//这句我也不明白具体时用来做什么。。。       

    comps = [calendar components:unitFlags fromDate:dateNow];     

    long weekNumber = [comps weekday]; //获取星期对应的长整形字符串

    long day=[comps day];//获取日期对应的长整形字符串

    long year=[comps year];//获取年对应的长整形字符串

    long month=[comps month];//获取月对应的长整形字符串

    long hour=[comps hour];//获取小时对应的长整形字符串

    long minute=[comps minute];//获取月对应的长整形字符串

    long second=[comps second];//获取秒对应的长整形字符串

 

持续更新中

posted on 2015-05-07 10:49  码农之上~  阅读(98)  评论(0编辑  收藏  举报

导航