iOS Standard time and timestamp convert each other

Source: Internet
Author: User

this article reprinted to http://blog.csdn.net/ios_che/article/details/8090977
  1. To set the time display format:
  2. nsstring* timestr = @"2011-01-26 17:40:50";
  3. NSDateFormatter *formatter = [[[[NSDateFormatter alloc] init] autorelease];
  4. [Formatter Setdatestyle:nsdateformattermediumstyle];
  5. [Formatter Settimestyle:nsdateformattershortstyle];
  6. [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
  7. //Set the time zone, which is sometimes important
  8. //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.
  9. //For example, if you publish for 2010-01-26 17:40:50, how much time should the user see in the UK and Ireland?
  10. //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 .
  11. nstimezone* TimeZone = [Nstimezone timezonewithname:@"Asia/shanghai"];
  12. [Formatter Settimezone:timezone];
  13. nsdate* date = [Formatter datefromstring:timestr]; //------------turn the string into NSDate by formatter
  14. NSDate *datenow = [NSDate Date]; //Time now, you can output to see what the format is
  15. NSString *nowtimestr = [Formatter stringfromdate:datenow]; //----------turn NSDate into NSString by formatter format
  16. Time-to-timestamp method:
  17. NSString *timesp = [NSString stringwithformat:@"%d", (long) [Datenow timeIntervalSince1970]];
  18. NSLog (@"timesp:%@", TIMESP); //timestamp value
  19. The method of time stamp turn time
  20. NSDate *CONFROMTIMESP = [NSDate datewithtimeintervalsince1970:1296035591];
  21. NSLog (@"1296035591 =%@", CONFROMTIMESP);
  22. NSString *confromtimespstr = [Formatter stringfromdate:confromtimesp];
  23. NSLog (@"confromtimespstr =%@", confromtimespstr);
  24. Time-Stamping Method:
  25. nsdateformatter* formatter = [[NSDateFormatter alloc] init];
  26. [Formatter Setdatestyle:nsdateformattermediumstyle];
  27. [Formatter Settimestyle:nsdateformattershortstyle];
  28. [Formatter setdateformat:@"YYYYMMDDHHMMSS"];
  29. NSDate *date = [Formatter datefromstring:@"1283376197"];
  30. NSLog (@"date1:%@", date);
  31. [Formatter release];

iOS Standard time and timestamp convert each other

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.