iOS timestamp string nsdate conversion demo

Source: Internet
Author: User
Tags dateformat goto string format

1 in the project we often have to do time format conversion, the following is my encapsulated nsdate+timecategory, hope to help everyone.


#import <Foundation/Foundation.h> @interface nsdate (timecategory)/** * String goto nsdate * * @param thetime string time * @par AM format conversion format such as Yyyy-mm-dd HH:mm:ss, i.e. 2015-07-15 15:00:00 * * @return < #return value description#> */+ (nsdate *) da   Tefromstring: (NSString *) timestr format: (NSString *) format;/** * NSDate turn timestamp * * @param date string Time * *   @return return timestamp */+ (Nsinteger) ctimestampfromdate: (NSDate *) date;/** * string to Timestamp * * @param thetime string time * @param format Conversion formats such as Yyyy-mm-dd HH:MM:SS, 2015-07-15 15:00:00 * * @return return timestamp string */+ (Nsinteger) ctimestampfromstring: (NSString *) Tim ESTR format: (NSString *) format;/** * Timestamp to String * * @param timeStamp timestamp * @param format conversion format such as Yyyy-mm-dd HH:mm:ss, 2015-07-15 15:00:00 * * @return return string format time */+ (NSString *) Datestrfromcstamptime: (Nsinteger) TimeStamp Withdateformat: (NSString *) format;/** * NSDate Turn String * * @param date nsdate time * @param fo Rmat conversion formats such as YYYY-MM-DD HH:mm:ss, i.e.2015-07-15 15:00:00 * * @return return string format time */+ (NSString *) Datestrfromdate: (NSDate *) Date Withdateformat: (NS String *) 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 *dateforma    tter = [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* Dateform    at = [NSDate defaultformatter];    [DateFormat Setdateformat:format]; return [DateFormat stringfromdate:date];}


2 calls

#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 = @ "2015-07-15 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 to string:%@->%@", date,timestr);}

Get the following results separately

2015-07-13 15:02:07.662 timedemo[2780:89582] String goto 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 Turn time stamp: 2015-07-13 07:02:14 +0000->1436770934

2015-07-13 15:02:20.077 timedemo[2780:89582] string turn timestamp: 2015-07-15 15:00:00-1436943600

2015-07-13 15:02:22.957 timedemo[2780:89582] Timestamp to string: 1436943600->2015-07-15 15:00:00

2015-07-13 15:02:25.901 timedemo[2780:89582] NSDate go to string: 2015-07-13 07:02:25 +0000->2015-07-13 15:02:25




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS timestamp string nsdate conversion demo

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.