NSDate Timestamp and string conversion

Source: Internet
Author: User

  1. One, the method of conversion is
  2. NSString *timesp = [NSString stringwithformat:@"%d", (long) [Localedate timeIntervalSince1970]];
  3. NSLog (@"timesp:%@", TIMESP); //timestamp value
  4. Second, convert the acquired time into the current time
  5. NSDate *datenow = [NSDate Date]; //Time now, you can output to see what the format is
  6. Nstimezone *zone = [Nstimezone systemtimezone];
  7. Nsinteger interval = [zone Secondsfromgmtfordate:datenow];
  8. NSDate *localedate = [Datenow datebyaddingtimeinterval:interval];
  9. NSLog (@"%@", localedate);
  10. 3. Ways to convert timestamps into time
  11. NSDate *CONFROMTIMESP = [nsdate dateWithTimeIntervalSince1970:1363948516];
  12. NSLog (@"1363948516 =%@", CONFROMTIMESP);
  13. Timer
  14. NSDate *datenow = [NSDate Date]; //Time now, you can output to see what the format is
  15. Nstimezone *zone = [Nstimezone systemtimezone];
  16. Nsinteger interval = [zone Secondsfromgmtfordate:datenow];
  17. NSDate *localedate = [Datenow datebyaddingtimeinterval:interval];
  18. NSLog (@"%@", localedate);
  19. NSString *timesp = [NSString stringwithformat:@"%lld", (long long) [Localedate timeIntervalSince1970]];
  20. NSLog (@"timesp:%@", TIMESP); //Timestamp value 1369189763711 1369218563 1369218614
  21. NSDate *CONFROMTIMESP = [nsdate dateWithTimeIntervalSince1970:1369189763711/1000];
  22. NSLog (@"1363948516 =%@", CONFROMTIMESP);
  23. //Instantiate a NSDateFormatter object
  24. //Judge yesterday morning time before the day of the hour stamp
  25. NSDateFormatter *dateformatter1 = [[[[NSDateFormatter alloc] init] autorelease];
  26. [DateFormatter1 setdateformat:@"Yyyy-mm-dd 00:00:00"];
  27. NSString *currentdatestr1 = [dateFormatter1 stringfromdate:[nsdate Date]];
  28. NSLog (@"wee Hours:%@", CURRENTDATESTR1);
  29. NSString *TIMESP1 = [NSString stringwithformat:@"%lld", (long long) [Localedate timeIntervalSince1970]];
  30. NSLog (@"Morning time stamp:%@", TIMESP1);
  31. //Yesterday morning time stamp
  32. NSString *timesp2 = [NSString stringwithformat:@"%lld", (long long) [Localedate timeintervalsince1970]-  24*60*60];
  33. NSLog (@"Yesterday morning Time stamp:%@", TIMESP2);

NSDate Timestamp and string conversion

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.