1 在项目中我们经常要做时间格式的转换,以下是我封装的NSDate+TimeCategory,希望帮助到大家。
#import <Foundation/Foundation.h> @interface NSDate (TimeCategory) /** * 字符串转NSDate * * @param theTime 字符串时间 * @param format 转化格式 如yyyy-MM-dd HH:mm:ss,即2015-07-15 15:00:00 * * @return <#return value description#> */ + (NSDate *)dateFromString:(NSString *)timeStr format:(NSString *)format; /** * NSDate转时间戳 * * @param date 字符串时间 * * @return 返回时间戳 */ + (NSInteger)cTimestampFromDate:(NSDate *)date; /** * 字符串转时间戳 * * @param theTime 字符串时间 * @param format 转化格式 如yyyy-MM-dd HH:mm:ss,即2015-07-15 15:00:00 * * @return 返回时间戳的字符串 */ +(NSInteger)cTimestampFromString:(NSString *)timeStr format:(NSString *)format; /** * 时间戳转字符串 * * @param timeStamp 时间戳 * @param format 转化格式 如yyyy-MM-dd HH:mm:ss,即2015-07-15 15:00:00 * * @return 返回字符串格式时间 */ + (NSString *)dateStrFromCstampTime:(NSInteger)timeStamp withDateFormat:(NSString *)format; /** * NSDate转字符串 * * @param date NSDate时间 * @param format 转化格式 如yyyy-MM-dd HH:mm:ss,即2015-07-15 15:00:00 * * @return 返回字符串格式时间 */ + (NSString *)datestrFromDate:(NSDate *)date withDateFormat:(NSString *)format; @end
#import "NSDate+TimeCategory.h" static NSDateFormatter *dateFormatter; @implementation NSDate (TimeCategory) +(NSDateFormatter *)defaultFormatter { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dateFormatter = [[NSDateFormatter alloc]init]; }); return dateFormatter; } + (NSDate *)dateFromString:(NSString *)timeStr format:(NSString *)format { NSDateFormatter *dateFormatter = [NSDate defaultFormatter]; [dateFormatter setDateFormat:format]; NSDate *date = [dateFormatter dateFromString:timeStr]; return date; } + (NSInteger)cTimestampFromDate:(NSDate *)date { return (long)[date timeIntervalSince1970]; } +(NSInteger)cTimestampFromString:(NSString *)timeStr format:(NSString *)format { NSDate *date = [NSDate dateFromString:timeStr format:format]; return [NSDate cTimestampFromDate:date]; } + (NSString *)dateStrFromCstampTime:(NSInteger)timeStamp withDateFormat:(NSString *)format { NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp]; return [NSDate datestrFromDate:date withDateFormat:format]; } + (NSString *)datestrFromDate:(NSDate *)date withDateFormat:(NSString *)format { NSDateFormatter* dateFormat = [NSDate defaultFormatter]; [dateFormat setDateFormat:format]; return [dateFormat stringFromDate:date]; }
2调用
#import "NSDate+TimeCategory.h"
- (IBAction)strToDate:(id)sender { NSString *timeStr = @"2015-07-15 15:00:00"; NSDate *date = [NSDate dateFromString:timeStr format:@"yyyy-MM-dd HH:mm:ss"]; NSLog(@"字符串转NSDate:%@ -> %@",timeStr,date); }
- (IBAction)dateToStamp:(id)sender { NSDate *date = [NSDate date]; NSInteger stamp = [NSDate cTimestampFromDate:date]; NSLog(@"NSDate转时间戳:%@ ->%ld",date,stamp); }
- (IBAction)strToStamp:(id)sender { NSString *timeStr = @"2015-07-15 15:00:00"; NSInteger stamp = [NSDate cTimestampFromString:timeStr format:@"yyyy-MM-dd HH:mm:ss"]; NSLog(@"字符串转时间戳:%@ -> %ld",timeStr,stamp); }
- (IBAction)stampToStr:(id)sender { NSInteger stamp = 1436943600; NSString *timeStr = [NSDate dateStrFromCstampTime:stamp withDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSLog(@"时间戳转字符串:%ld ->%@",stamp,timeStr); }
- (IBAction)dateToStr:(id)sender { NSDate *date = [NSDate date]; NSString *timeStr = [NSDate datestrFromDate:date withDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSLog(@"NSDate转字符串:%@ ->%@",date,timeStr); }
分别得到以下结果
2015-07-13 15:02:07.662 TimeDemo[2780:89582]
字符串转NSDate:2015-07-15 15:00:00 -> 2015-07-15 07:00:00 +0000
2015-07-13 15:02:14.653 TimeDemo[2780:89582] NSDate转时间戳:2015-07-13
07:02:14 +0000 ->1436770934
2015-07-13 15:02:20.077 TimeDemo[2780:89582]
字符串转时间戳:2015-07-15 15:00:00 -> 1436943600
2015-07-13 15:02:22.957 TimeDemo[2780:89582]
时间戳转字符串:1436943600 ->2015-07-15 15:00:00
2015-07-13 15:02:25.901 TimeDemo[2780:89582] NSDate转字符串:2015-07-13
07:02:25 +0000 ->2015-07-13 15:02:25
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 16:06:01