獲取字典里的時間戳 轉換為時間
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_dict[@"ctime"] doubleValue]/1000];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *stringTime = [formatter stringFromDate:date];
self.timeLabel.text = stringTime;
獲取當前時間的方法
-(NSString *)timeDate{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSString *string = [formatter stringFromDate:date];
return string;
}
獲取當前時間戳
-(NSString *)timeDate{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval timestamp = [dat timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%.0f", timestamp];
return timeString;
}
獲得當前:年、月、日
//這個是NSDate類型的日期,所要獲取的年月日都放在這里;
NSDate *date = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
//這句是說你要獲取日期的元素有哪些。獲取年就要寫NSYearCalendarUnit,獲取小時就要寫NSHourCalendarUnit,中間用|隔開;
unsigned int unitFlags = NSCalendarUnitMonth;
//把要從date中獲取的unitFlags標示的日期元素存放在NSDateComponents類型的d里面;
NSDateComponents *d = [cal components:unitFlags fromDate:date];
//然后就可以從d中獲取具體的年月日了;
NSInteger month = [d month];
勤學如早春之苗,不見其增,日有所漲。
輟學如磨刀之石,不見其減,日有所損。