1. 获得当前的时间(NSDate):
+ (NSDate *)currentDate {
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localDate = [date dateByAddingTimeInterval: interval];
return localDate;
}
PS: 有些人直接用[NSDate date];返回的数据作为当前的时间,这样做获得的时间可能会有错误, 原因是因为时区的问题, 所以应该获得并设置当前所在的时区, [NSTimeZone timeZoneWithName:@"shanghai"]也是设置时区的方法;
2. 获取从某个日期开始往前或者往后多久的日期(NSDate):
+ (NSDate *)specificDate {
// 此处600代表600秒,如果需要获取之前的,将600改为-600即可
// 类似的方法 [[NSDate alloc]initWithTimeIntervalSinceNow:60];
// [[NSDate alloc]initWithTimeIntervalSince1970:600];
NSDate *date = [self currentDate];
return [[NSDate alloc] initWithTimeInterval:600 sinceDate:date];
}
3. 按照一定的格式获得当前的时间(NSString):
+ (NSString *)currentString {
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
// 设置时区
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
// 设置日期格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
return dateString;
}
4. 获得当前时间的时间戳(NSInteger):
+ (NSInteger)currentTimeStamp {
NSInteger timeStamp = [[self currentDate] timeIntervalSince1970];
return timeStamp;
}
5. 时间戳转化为时间(NSInteger --> NSDate):
+(NSDate *)timeStampChangeToDate:(NSInteger)timeStamp {
NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
return date;
}
6. 时间转化为时间戳(NSDate --> NSInteger):
+ (NSInteger)dateChangeToTimestamp:(NSDate *)date {
NSInteger timeStamp = [date timeIntervalSince1970];
return timeStamp;
}
7. 格式化的时间转化为时间戳
- (NSString *)dateChangeTime:(NSString *)dateString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日";
NSDate *createDate = [formatter dateFromString:dateString];
NSTimeInterval a=[createDate timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
return timeString;
}
PS:这样获取的时间戳并不是某一时刻的时间戳, 应该是dd日零点的, 希望大家指出错误
8. 获得某一时间为星期几(NSDate --> NSString):
+ (NSString *)getWeekDayByDate:(NSDate *)date{
NSCalendar *calendar = [NSCalendar currentCalendar];
//NSDateComponents *weekComp = [calendar components:NSWeekdayCalendarUnit fromDate:date];
NSDateComponents *weekComp = [calendar components:NSCalendarUnitWeekday fromDate:date];
NSInteger weekDayEnum = [weekComp weekday];
NSString *weekDays = nil;
switch (weekDayEnum) {
case 1:
weekDays = @"星期日";
break;
case 2:
weekDays = @"星期一";
break;
case 3:
weekDays = @"星期二";
break;
case 4:
weekDays = @"星期三";
break;
case 5:
weekDays = @"星期四";
break;
case 6:
weekDays = @"星期五";
break;
case 7:
weekDays = @"星期六";
break;
default:
break;
}
return weekDays;
}
9. 时间转化为农历表示(NSDate --> NSString):
+(NSString*)getChineseCalendarWithDate:(NSDate *)date{
NSArray *chineseYears = [NSArray arrayWithObjects:
@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
@"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未",
@"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
@"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑",
@"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
@"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil];
NSArray *chineseMonths=[NSArray arrayWithObjects:
@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
@"九月", @"十月", @"冬月", @"腊月", nil];
NSArray *chineseDays=[NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
NSLog(@"%zi_%zi_%zi %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);
NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
NSString *chineseCal_str =[NSString stringWithFormat: @"农历:%@年%@%@",y_str,m_str,d_str];
return chineseCal_str;
}