IOS: Time formatting (standard time to timestamp, timestamp to standard time, timestamp to date)

Source: Internet
Author: User
Tags local time timestamp to date

Create a new NSString (time) classification, creating a conversion of class method implementation times

. h file

#import <Foundation/Foundation.h>@interface  nsstring (kjtime)//  Timestamp---> Time + (NSString *) Transtotime: (NSString *) Timsp; // Timestamp ---> Date + (NSString *) Transtodate: (NSString *) Timsp; // time----> timestamp + (NSString *) Transtotimesp: (NSString *) time; @end

. m file

#import "nsstring+kjtime.h"@implementationnsstring (kjtime)//time Stamp---> Time+ (NSString *) Transtotime: (NSString *) timsp{Nstimeinterval Time=[timsp Doublevalue];//If you do not use the local time zone, because the jet lag is 8 hours. = = 28800 secNSDate *detaildate=[NSDate Datewithtimeintervalsince1970:time]; //instantiate a NSDateFormatter objectNSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone];//set local time zone//Set the time format, where you can set the format you want[Dateformatter Setdateformat:@"MM/DD hh:mm"]; NSString*currentdatestr =[Dateformatter stringfromdate:detaildate]; returncurrentdatestr;}//Timestamp ---> Date+ (NSString *) Transtodate: (NSString *) timsp{Nstimeinterval Time=[timsp Doublevalue];//If you do not use the local time zone, because the jet lag is 8 hours. = = 28800 secNSDate *detaildate=[NSDate Datewithtimeintervalsince1970:time]; //instantiate a NSDateFormatter objectNSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone];//set local time zone//Set the time format, where you can set the format you want[Dateformatter Setdateformat:@"mm month DD Day"]; NSString*currentdatestr =[Dateformatter stringfromdate:detaildate]; returncurrentdatestr;}//time----> Time stamp+ (NSString *) Transtotimesp: (NSString *) time{NSDateFormatter*dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Settimezone:[nstimezone Localtimezone]; //set local time zone[Dateformatter Setdateformat:@"YYYY-MM-DD hh:mm"]; NSDate*date =[Dateformatter Datefromstring:time]; NSString*TIMESP = [NSString stringWithFormat:@"%ld", (Long) [date timeIntervalSince1970]];//time Stamp    returnTimesp;}@end

IOS: Time formatting (standard time to timestamp, timestamp to standard time, timestamp to date)

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.