今天整理一下时间转换的类。
1、获取当前时间字符串
/**
* 当前时间字符串
*
* @param format 时间格式 @"yyyy-MM-dd HH:mm:ss"
*
* @return 时间字符串
*/
+ (NSString *)dateStringWithFormat:(NSString *)format{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
//[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString = [dateFormatter stringFromDate:now];
return dateString;
}
2、用格式:时间获取正确时区的时间字符串
/**
* 用格式:时间获取正确时区的时间字符串
*
* @param format 时间格式 @"yyyy-MM-dd HH:mm:ss"
* @param date 时间
*
* @return 时间字符串
*/
+ (NSString *)dateStringWithFormat:(NSString *)format date:(NSDate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
//[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
//[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
3、时间字符串 获取格式的时间字符串
/**
* 时间字符串 获取格式的时间字符串
*
* @param format 时间格式
* @param date 时间
*
* @return 时间字符串
*/
+ (NSString *)dateStringWithFormat:(NSString *)format dateString:(NSString *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSDate *fDate = [formatter dateFromString:date];
return [formatter stringFromDate:fDate];
}
4、时间 距离现在的天数
/**
* 时间 距离现在的天数
*
* @param date 时间
*
* @return 返回天数
*/
+ (int)daysFromDate:(NSDate *)date{
NSCalendarUnit unit = NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// NSDateComponents *start = [gregorian components:unit fromDate:[NSDate date]];
// NSDateComponents *end = [gregorian components:unit fromDate:date];
return (int)[[gregorian components:unit fromDate:date toDate:[NSDate date] options:0] day];
}
5、获取距离当前的时间 聊天时间
/**
* 获取距离当前的时间 聊天时间
*
* @param date 日期
*
* @return 日期-昨天-前天-2015-01-01
*/
+ (NSString *)dateStringForChat:(NSDate *)date{
int days = [self daysFromDate:date];
NSString *dateString;
if (days == 0) {
dateString = [self dateStringWithFormat:@"HH:mm" date:date];
}else if (days == 1) {
dateString = [NSString stringWithFormat:@"昨天 %@",[self dateStringWithFormat:@"HH:mm" date:date]];
}else if (days == 2) {
dateString = [NSString stringWithFormat:@"前天 %@",[self dateStringWithFormat:@"HH:mm" date:date]];
}else {
dateString = [self dateStringWithFormat:@"yyyy-MM-dd HH:mm" date:date];
}
return dateString;
}
6、NSNumber转换成时间字符串
/**
* NSNumber转换成时间字符串
*
* @param format 日期格式
* @param number 时间NSNumber
*
* @return 时间字符串
*/
+ (NSString *)dateStringWithFormat:(NSString *)format number:(NSNumber *)number{
NSString*str=nil;//时间戳
if (![number isKindOfClass:[NSNumber class]]) {
str=(NSString*)number;//时间戳
}else{
str=[number stringValue];//时间戳
}
NSTimeInterval time =[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time/1000];
//实例化一个NSDateFormatter对象
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:format];
NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
return currentDateStr;
}
7、时间字符串转换成时间字符串
/**
* 时间字符串转换成时间字符串
*
* @param format 日期格式
* @param string 时间字符串
*
* @return 时间字符串
*/
+ (NSString *)dateStringWithStringFormat:(NSString *)format dateString:(NSString *)string{
id result;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
result=[formatter numberFromString:string];
if(result)
{
double timesTamp = [string doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timesTamp/1000];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
//[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
NSString*confromTimespStr = [dateFormatter stringFromDate:date];
return confromTimespStr;
}
return nil;
}
8、获取某年某月的天数
/**
* 获取某年某月的天数
*
* @param year 年
* @param month 月
*
* @return 多少天
*/
+ (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
return 31 ;
if((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;
if((year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3))
{
return 28;
}
if(year % 400 == 0)
return 29;
if(year % 100 == 0)
return 28;
return 29;
}
9、在当前日期时间加上 某个时间段(传负数即返回当前时间之前x月x日的时间)
/**
* ** 在当前日期时间加上 某个时间段(传负数即返回当前时间之前x月x日的时间)
*
* @param year 当前时间若干年后 (传负数为当前时间若干年前)
* @param month 当前时间若干月后 (传0即与当前时间一样)
* @param day 当前时间若干天后
* @param hour 当前时间若干小时后
* @param minute 当前时间若干分钟后
* @param second 当前时间若干秒后
*
* @return 处理后的时间字符串
*/
+ (NSString *)dateStringAfterlocalDateForYear:(NSInteger)year Month:(NSInteger)month Day:(NSInteger)day Hour:(NSInteger)hour Minute:(NSInteger)minute Second:(NSInteger)second
{
// 当前日期
NSDate *localDate = [NSDate date]; // 为伦敦时间
// 在当前日期时间加上 时间:格里高利历
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *offsetComponent = [[NSDateComponents alloc]init];
[offsetComponent setYear:year ]; // 设置开始时间为当前时间的前x年
[offsetComponent setMonth:month];
[offsetComponent setDay:day];
[offsetComponent setHour:(hour+8)]; // 中国时区为正八区,未处理为本地,所以+8
[offsetComponent setMinute:minute];
[offsetComponent setSecond:second];
// 当前时间后若干时间
NSDate *minDate = [gregorian dateByAddingComponents:offsetComponent toDate:localDate options:0];
NSString *dateString = [NSString stringWithFormat:@"%@",minDate];
return dateString;
}
10、日期格式转换
/************
日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];
***********/
+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
int ci;
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dt1;
NSDate *dt2;
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result){
//date02比date01大
case NSOrderedAscending: ci=1;break;
//date02比date01小
case NSOrderedDescending: ci=-1;break;
//date02=date01
case NSOrderedSame: ci=0;break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1);break;
}
return ci;
}
11、时间格式转换
/**
时间格式转换
@param targetFormat 目标字符串格式
@param changeFormat 改变字符串格式
@param valueStr 需要改变的字符串
@return 返回字符串
*/
+(NSString *)endStringFormatTargetFormat:(NSString *)targetFormat changeFormatStrFormat:(NSString *)changeFormat andValueStr:(NSString *)valueStr{
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:targetFormat];
NSDate *date =[dateFormat dateFromString:valueStr];
NSDateFormatter* dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:changeFormat];
NSString *publishtimeStr = [dateFormat2 stringFromDate:date];
return publishtimeStr;
}
12、判断是否在工作时间内
/**
判断是否在工作时间内
@param fromdate 开始时间
@param toDate 结束时间
@return 判断结果
*/
+(BOOL)inWorkPeriodFromDate:(NSString *)fromdate toDate:(NSString *)toDate {
NSString *dateStr = [self obtainCurrentTimeWithFormatter:@"yyyy-MM-dd HH:mm:ss"];
NSString *from = [NSString stringWithFormat:@"%@ %@",dateStr,fromdate];
NSString *to = [NSString stringWithFormat:@"%@ %@",dateStr,toDate];
NSTimeInterval fromTime = [[self transfromDateFromString:from inFormatter:@"yyyy-MM-dd HH:mm:ss"] timeIntervalSince1970];
NSTimeInterval toTime = [[self transfromDateFromString:to inFormatter:@"yyyy-MM-dd HH:mm:ss"] timeIntervalSince1970];
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
if (currentTime >fromTime && currentTime < toTime) {
return YES;
NSLog(@"是当前时间区内");
}else {
return NO;
}
}
13、获取当月第一天
//获取当月第一天 dateStr 传 @"yyyy-MM-dd"
+ (NSString *)getMonthFirstAndLastDayWith:(NSString *)dateStr{
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSDate *newDate=[format dateFromString:dateStr];
double interval = 0;
NSDate *firstDate = nil;
NSDate *lastDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
if (OK) {
lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
}else {
return @"";
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *firstString = [myDateFormatter stringFromDate: firstDate];
//获取当月的最后一天
// NSString *lastString = [myDateFormatter stringFromDate: lastDate];
return firstString;
// return @[firstString, lastString];
}
END.