Conversion between NSDate, NSString, and timestamp in iOS development, nsdatensstring

Source: Internet
Author: User

Conversion between NSDate, NSString, and timestamp in iOS development, nsdatensstring

1 // convert UTCDate (the World Standard Time) to the standard Date of the local time zone (the time displayed by the clock) 2 // NSDate * date = [NSDate date Date]; 06:54:41 + 0000 3 // After conversion: 14:54:41 + 0000 4-(NSDate *) getLocalDateFromUTCDate :( NSDate *) UTCDate {5 6 NSTimeZone * tz = [NSTimeZone ultulttimezone]; 7 NSInteger seconds = [tz secondsFromGMTForDate: UTCDate]; 8 return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate]; 9 10} 11 12 // convert the standard Date of the local time zone to UTCDate 13 // the current standard time of the local area: 14:54:41 + 0000 14 // convert it to the world standard time: 06:54:41 + 0000 15-(NSDate *) Expiration :( NSDate *) LocalDate {16 17 NSTimeZone * tz = [NSTimeZone defaultTimeZone]; 18 NSInteger seconds =-[tz secondsFromGMTForDate: LocalDate]; 19 return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate]; 20 21} 22 23 // obtain the current time String Based on UTCDate (time displayed on the clock) 24 // input: [NSDate date] 07:44:05 + 0000 25 // output: 15:44:05 26-(NSString *) localStringFromUTCDate :( NSDate *) UTCDate {27 28 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 29 NSTimeZone * tz = [NSTimeZone defaultTimeZone]; 30 [dateFormatter setTimeZone: tz]; 31 [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 32 NSString * result = [dateFormatter stringFromDate: UTCDate]; 33 return result; 34 35} 36 37 // obtain the current time string (time displayed on the clock) based on the UTC string 38 // input: 07:44:05 39 // output: 15:44:05 40-(NSString *) localStringFromUTCString :( NSString *) UTCString {41 42 // convert the UTC string to UTCDate first; 43 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 44 NSTimeZone * tz = [NSTimeZone timeZoneWithAbbreviation: @ "GMT"]; 45 [dateFormatter setTimeZone: tz]; 46 [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss "]; 47 NSDate * UTCDate = [dateFormatter dateFromString: UTCString]; 48 49 [dateFormatter setTimeZone: [NSTimeZone defaultTimeZone]; 50 NSString * result = [dateFormatter stringFromDate: UTCDate]; 51 return result; 52} 53 54 // convert the current time string to UTCDate 55-(NSDate *) UTCDateFromLocalString :( NSString *) localString {56 57 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 58 [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 59 NSDate * date = [dateFormatter dateFromString: localString]; 60 return date; 61} 62 63 // convert the current time string to the UTC string 64-(NSString *) UTCStringFromLocalString :( NSString *) localString {65 66 NSDate * date = [self UTCDateFromLocalString: localString]; 67 NSString * string = [NSString stringWithFormat: @ "% @", date]; 68 NSString * result = [string substringToIndex: string. length-6]; 69 return result; 70 71} 72 73 // UTCDate to UTC string 74-(NSString *) UTCStringFromUTCDate :( NSDate *) UTCDate {75 76 NSDateFormatter * dataFormatter = [[NSDateFormatter alloc] init]; 77 [dataFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 78 NSTimeZone * tz = [NSTimeZone timeZoneWithAbbreviation: @ "GMT"]; 79 [dataFormatter setTimeZone: tz]; 80 NSString * UTCString = [dataFormatter stringFromDate: UTCDate]; 81 return UTCString; 82 83} 84 85 // convert the current time (UTCDate) to the timeStampFromUTCDate (NSDate *) UTCDate {87 88 NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970]; 89 // * 1000, accurate to milliseconds; precise to seconds; 90 NSString * result = [NSString stringWithFormat: @ "%. 0f ", timeInterval]; 91 return result; 92 93} 94 95 // convert the current time string (time displayed on the clock) to the time stamp 96-(NSString *) timeStamapFromLocalString :( NSString *) localString {97 98 // convert it to UTCDate 99 NSDate * UTCDate = [self UTCDateFromLocalString: localString]; 100 NSString * timeStamap = [self accept: UTCDate]; 101 return timeStamap; 102 103} 104 105 // convert UTCString to timeStamapFromUTCString (NSString *) UTCString {106 107 // convert the UTC string to UTCDate first; 109 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 110 NSTimeZone * tz = [NSTimeZone timeZoneWithAbbreviation: @ "GMT"]; 111 [dateFormatter setTimeZone: tz]; 112 [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 113 NSDate * UTCDate = [dateFormatter dateFromString: UTCString]; 114 115 NSString * timeStamap = [self timeStampFromUTCDate: UTCDate]; 116 return timeStamap; 117} 118 119 // time stamp to UTCDate120-(NSDate *) UTCDateFromTimeStamap :( NSString *) timeStamap {121 122 NSTimeInterval timeInterval = [timeStamap doubleValue]; 123 // 1000; if the input timeStamap is accurate to milliseconds, remember to/1000124 NSDate * UTCDate = [NSDate dateWithTimeIntervalSince1970: timeInterval]; 125 return UTCDate; 126 127}

If you want to convert the timestamp into a string, convert it based on the obtained UTCDate.

 

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.