iOS Standard time and timestamp convert each other(2012-07-18 17:03:34)
reproduced
Tags: ios timestamp Standard Time format conversion Talk |
Category: iphone development |
Set 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 //set time zone, this is sometimes important for time processing //For example, 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];//now time, you can output to see what the format is NSString *nowtimestr = [Formatter stringfromdate:datenow];//----------to convert nsdate in formatter format to nsstring time to time stamp method: nsstring *TIMESP = [NSString stringwithformat:@ "%d", (long) [Datenow timeIntervalSince1970]]; nslog (@ "timesp:%@", TIMESP); The timestamp value method of time stamp nsdate *confromtimesp = [NSDate dateWithTimeIntervalSince1970 : 1296035591]; nslog (@ "1296035591 =%@", CONFROMTIMESP); nsstring *confromtimespstr = [Formatter stringFromDate: Confromtimesp]; nslog (@ "confromtimespstr = %@", Confromtimespstr), method of time-stamp transfer:
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