this article reprinted to http://blog.csdn.net/ios_che/article/details/8090977
- To set the time display format:
- nsstring* timestr = @"2011-01-26 17:40:50";
- NSDateFormatter *formatter = [[[[NSDateFormatter alloc] init] autorelease];
- [Formatter Setdatestyle:nsdateformattermediumstyle];
- [Formatter Settimestyle:nsdateformattershortstyle];
- [Formatter setdateformat:@"Yyyy-mm-dd HH:mm:ss"]; //----------Set the format you want, the difference between HH and HH: 12-hour, 24-hour system, respectively
- //Set the time zone, which is sometimes important
- //For example, if you publish information in the country, the user in another time zone abroad, you want to let the user see the correct release time to pay attention to time zone settings, time conversion.
- //For example, if you publish for 2010-01-26 17:40:50, how much time should the user see in the UK and Ireland?
- //They have a 7-hour time difference with us, so they're not there yet. That's what's going to happen in the future .
- nstimezone* TimeZone = [Nstimezone timezonewithname:@"Asia/shanghai"];
- [Formatter Settimezone:timezone];
- nsdate* date = [Formatter datefromstring:timestr]; //------------turn the string into NSDate by formatter
- NSDate *datenow = [NSDate Date]; //Time now, you can output to see what the format is
- NSString *nowtimestr = [Formatter stringfromdate:datenow]; //----------turn NSDate into NSString by formatter format
- Time-to-timestamp method:
- NSString *timesp = [NSString stringwithformat:@"%d", (long) [Datenow timeIntervalSince1970]];
- NSLog (@"timesp:%@", TIMESP); //timestamp value
- The method of time stamp turn time
- NSDate *CONFROMTIMESP = [NSDate datewithtimeintervalsince1970:1296035591];
- NSLog (@"1296035591 =%@", CONFROMTIMESP);
- NSString *confromtimespstr = [Formatter stringfromdate:confromtimesp];
- NSLog (@"confromtimespstr =%@", confromtimespstr);
- Time-Stamping Method:
- nsdateformatter* formatter = [[NSDateFormatter alloc] init];
- [Formatter Setdatestyle:nsdateformattermediumstyle];
- [Formatter Settimestyle:nsdateformattershortstyle];
- [Formatter setdateformat:@"YYYYMMDDHHMMSS"];
- NSDate *date = [Formatter datefromstring:@"1283376197"];
- NSLog (@"date1:%@", date);
- [Formatter release];
iOS Standard time and timestamp convert each other