Date string converted to NSDate

Source: Internet
Author: User
Tags date1 set time

1     //Pure Numeric Date2NSString *STR1 =@"20150611132535";3     //Date String4NSString *STR2 =@"2015/05/12 10:22:01";5     //Date string with time zone6NSString *STR3 =@"Tue Sep 21:37:19 +0800";7     //date string with no time zone8NSString *STR4 =@"Tue Sep 21:15:27";9     //GMTTenNSString *STR5 =@"Wed, 4 Nov 03:25:31 GMT"; One      A     //Create date Format Object -NSDateFormatter *format =[[NSDateFormatter alloc] init]; -     //set up a region the[Format Setlocale:[[nslocale alloc] Initwithlocaleidentifier:@"en_US"]]; -      -     //str1 -[Format Setdateformat:@"YYYYMMDDHHMMSS"]; +NSDate *date1 =[format datefromstring:str1]; -NSLog (@"%@", date1); +      A     //str2 at[Format Setdateformat:@"YYYY/MM/DD HH:mm:ss"]; - //[Format setdateformat:@ "yyy/mm/d h:m:ss"]; - //[Format setdateformat:@ "Yyyy/mm/dd h:m:ss"]; -NSDate *date2 =[format DATEFROMSTRING:STR2]; -NSLog (@"%@", date2); -      in     //STR3 -     //The following methods may appear to be problematic after iOS8, so another way to add a date method using the C language format to[Format Setdateformat:@"EEE MMM dd HH:mm:ss zzz yyyy"]; +NSDate *date3 =[format DATEFROMSTRING:STR3]; -NSLog (@"%@", date3); the      *     structTM sometime; $     Const Char*formatstring ="%a%b%d%h:%m:%s%z%Y";Panax NotoginsengStrptime ([Str3 utf8string], formatString, &sometime); -Date3 = [NSDate datewithtimeintervalsince1970:mktime (&sometime)]; theNSLog (@"%@", date3); +      A     //STR4 the[Format Setdateformat:@"EEE MMM dd HH:mm:ss yyyy"]; +NSDate *date4 =[format DATEFROMSTRING:STR4]; -NSLog (@"%@", date4); $      $     //STR5 -NSDateFormatter *FORMATGMT =[[NSDateFormatter alloc] init]; -     //Set time zone the[Formatgmt Setlocale:[[nslocale alloc] Initwithlocaleidentifier:@"En_us_posix"]]; -[Formatgmt Setdateformat:@"EEE, DD MMM yyy HH:mm:ss zzz"];WuyiNSDate *date5 =[Formatgmt DATEFROMSTRING:STR5]; theNSLog (@"%@", Date5);

Defining functions Function description function return value
time_t mktime (struct TM *timeptr);

Converts the time structure data to the elapsed number of seconds.

The Mktime () is used to convert the TM structure data referred to by the parameter timeptr to the number of seconds elapsed since the UTC time of January 1, 1970 0:0 0 seconds.

Returns the number of seconds elapsed
Char *strptime (const char *BUF, const char *format, struct TM *timeptr);

Strptime is to format a string as a tm struct type

Returns a date string that has not been formatted
+ (ID) dateWithTimeIntervalSince1970: (nstimeinterval) secs;

Returns the 1970/01/01 GMT as the benchmark, and then passes the time of secs seconds

NSDate Date Object

Date string converted to NSDate

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.