IOS timestamp string NSdate conversion demo

Source: Internet
Author: User

IOS timestamp string NSdate conversion demo

1. In projects, we often need to convert the time format. The following is my encapsulated NSDate + TimeCategory, hoping to help you.

 

 

# Import
 
  
@ Interface NSDate (TimeCategory)/*** convert string to NSDate ** @ param theTime string time * @ param format Conversion format such as yyyy-MM-dd HH: mm: ss, that is, 2015-07-15 15:00:00 ** @ return <# return value description #> */+ (NSDate *) dateFromString :( NSString *) timeStr format :( NSString *) format; /*** NSDate to timestamp ** @ param date string time ** @ return returns the timestamp */+ (NSInteger) cTimestampFromDate :( NSDate *) date; /*** string to timestamp *** @ param theTime string time * @ param format Conversion format such as yyyy-MM-dd HH: mm: ss, that is, 15:00:00 ** @ return returns the timestamp string */+ (NSInteger) cTimestampFromString :( NSString *) timeStr format :( NSString *) format; /*** convert the timeStamp to the string *** @ param timeStamp * @ param format Conversion format, for example, yyyy-MM-dd HH: mm: ss, that is, 15:00:00 ** @ return returns the string format time */+ (NSString *) dateStrFromCstampTime :( NSInteger) timeStamp withDateFormat :( NSString *) format; /*** convert NSDate to string *** @ param date NSDate time * @ param format Conversion format, for example, yyyy-MM-dd HH: mm: ss, that is, 15:00:00 ** @ return returns the string format time */+ (NSString *) datestrFromDate :( NSDate *) date withDateFormat :( NSString *) format; @ end
 

#import NSDate+TimeCategory.hstatic 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 call

# 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 (@ string to NSDate: % @-> % @, timeStr, date );}
-(IBAction) dateToStamp :( id) sender {NSDate * date = [NSDate date]; NSInteger stamp = [NSDate cTimestampFromDate: date]; NSLog (@ NSDate to timestamp: % @-> % ld, date, stamp );}
-(IBAction) strToStamp :( id) sender {NSString * timeStr = @ 15:00:00; NSInteger stamp = [NSDate cTimestampFromString: timeStr format: @ yyyy-MM-dd HH: mm: ss]; NSLog (@ string to timestamp: % @-> % ld, timeStr, stamp );}
-(IBAction) stampToStr :( id) sender {NSInteger stamp = 1436943600; NSString * timeStr = [NSDate dateStrFromCstampTime: stamp withDateFormat: @ yyyy-MM-dd HH: mm: ss]; NSLog (@ timestamp to string: % 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 convert string: % @-> % @, date, timeStr );}

The following results are displayed:

 

 

15:02:07. 662 TimeDemo [2780: 89582]String ConversionNSDate:15:00:00-> 07:00:00 + 0000

15:02:14. 653 TimeDemo [2780: 89582] NSDateTimestamp:07:02:14 + 0000-> 1436770934

15:02:20. 077 TimeDemo [2780: 89582]String to timestamp:15:00:00-> 1436943600

15:02:22. 957 TimeDemo [2780: 89582]Timestamp to string:1436943600-> 15:00:00

15:02:25. 901 TimeDemo [2780: 89582] NSDateConversion string:07:02:25 + 0000-> 15:02:25



 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.