#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd HH:mm:ss")
//获取当前日期,时间
+(NSDate *)getCurrentDate{
NSDate *now = [NSDate date];
return now;
}
//将日期转换为字符串(日期,时间)
+(NSString *)getDateStringFromDate:(NSDate *)date{
NSInteger location = 0;
NSString *timeStr = @"";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setDateFormat:@"HH:mm:a"];
NSString *ampm = [[[formatter stringFromDate:date] componentsSeparatedByString:@":"] objectAtIndex:2];
timeStr = [formatter stringFromDate:date];
NSRange range = [timeStr rangeOfString:[NSString stringWithFormat:@":%@",ampm]];
location = range.location;
NSString *string = [timeStr substringToIndex:location];
timeStr = [NSString stringWithFormat:@"%@ %@",ampm,string];
NSString *dateStr = @"";
NSDateFormatter *Dformatter = [[[NSDateFormatter alloc] init] autorelease];
[Dformatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[Dformatter setDateFormat:@"yyyy-MM-dd"];
dateStr = [Dformatter stringFromDate:date];
// NSLog(@"%@", [NSString stringWithFormat:@"%@ %@",dateStr,timeStr]);
return [NSString stringWithFormat:@"%@ %@",dateStr,timeStr];
}
//计算两个日期之间的差距,过了多少天。。
+(NSInteger)getDateToDateDays:(NSDate *)date withSaveDate:(NSDate *)saveDate{
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:date toDate:saveDate options:0];
NSInteger diffDay = [ cps day ];
[chineseClendar release];
return diffDay;
}
例:NSDate *lastDate = [self getSaveDate];//saveDate通过将NSDate转换为NSString来保存
currentDate = [NSDate date];
NSInteger day = [DateHelper getDateToDateDays:currentDate withSaveDate: lastDate];
//日期转字符串
+ (NSString * )NSDateToNSString: (NSDate * )date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
return dateString;
}
//字符串转日期
+ (NSDate * )NSStringToNSDate: (NSString * )string
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSDate *date = [formatter dateFromString :string];
[formatter release];
return date;
}
//1970年到现在的秒数转换成时间显示
- (NSString *)bySecondGetDate:(NSString *)second{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *dateLoca = [NSString stringWithFormat:@"%@",second];
NSTimeInterval time=[dateLoca doubleValue];
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [formatter stringFromDate:detaildate];
return timestr;
}
//获取当前日期,时间
+(NSDate *)getCurrentDate{
NSDate *now = [NSDate date];
return now;
}
//将日期转换为字符串(日期,时间)
+(NSString *)getDateStringFromDate:(NSDate *)date{
NSInteger location = 0;
NSString *timeStr = @"";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setDateFormat:@"HH:mm:a"];
NSString *ampm = [[[formatter stringFromDate:date] componentsSeparatedByString:@":"] objectAtIndex:2];
timeStr = [formatter stringFromDate:date];
NSRange range = [timeStr rangeOfString:[NSString stringWithFormat:@":%@",ampm]];
location = range.location;
NSString *string = [timeStr substringToIndex:location];
timeStr = [NSString stringWithFormat:@"%@ %@",ampm,string];
NSString *dateStr = @"";
NSDateFormatter *Dformatter = [[[NSDateFormatter alloc] init] autorelease];
[Dformatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[Dformatter setDateFormat:@"yyyy-MM-dd"];
dateStr = [Dformatter stringFromDate:date];
// NSLog(@"%@", [NSString stringWithFormat:@"%@ %@",dateStr,timeStr]);
return [NSString stringWithFormat:@"%@ %@",dateStr,timeStr];
}
//计算两个日期之间的差距,过了多少天。。
+(NSInteger)getDateToDateDays:(NSDate *)date withSaveDate:(NSDate *)saveDate{
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:date toDate:saveDate options:0];
NSInteger diffDay = [ cps day ];
[chineseClendar release];
return diffDay;
}
例:NSDate *lastDate = [self getSaveDate];//saveDate通过将NSDate转换为NSString来保存
currentDate = [NSDate date];
NSInteger day = [DateHelper getDateToDateDays:currentDate withSaveDate: lastDate];
//日期转字符串
+ (NSString * )NSDateToNSString: (NSDate * )date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
return dateString;
}
//字符串转日期
+ (NSDate * )NSStringToNSDate: (NSString * )string
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSDate *date = [formatter dateFromString :string];
[formatter release];
return date;
}
//1970年到现在的秒数转换成时间显示
- (NSString *)bySecondGetDate:(NSString *)second{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *dateLoca = [NSString stringWithFormat:@"%@",second];
NSTimeInterval time=[dateLoca doubleValue];
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [formatter stringFromDate:detaildate];
return timestr;
}