0524. Time Stamp conversion and time mutual conversion in Objective-C [Foundation]

Source: Internet
Author: User

What is a timestamp?

Timestamp is usually a string that uniquely identifies the time at a given moment. The digital timestamp technology is a variant of the digital signature technology.

Thinking: Simply put, it is based on the digest generated after file hash encryption and the time creden generated. Its value is the total number of milliseconds from January 1, January 1, 1970 to the current time.

In Objective-C, how does one convert the timestamp to NSDate?

NSString * str = @ "1283376197 ";

NSTimeIntervaltime = [str doubleValue];

NSDate * detaildate = [NSDate dateWithTimeIntervalSince1970: time];

NSLog (@ "date: % @", [detaildatedescription]);

Thinking: using this function, pass the double value of the timestamp to get the NSDate time.

How do I convert the current time to a timestamp?

NSDate * datenow = [NSDate date]; // The current time. You can output the following format:

NSTimeZone * zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: datenow];

NSDate * localeDate = [datenow dateByAddingTimeInterval: interval];

NSString * timeSp = [NSString stringWithFormat: @ "% d", (long) [localeDate timeIntervalSince1970];

NSLog (@ "timeSp: % @", timeSp); // The timestamp Value

Thinking: Get the Interval of the standard time according to the current time and the time zone of the system, then get the time localeDate after the verification, and finally [localeDate timeIntervalSince1970] Get the difference between the time after the verification and the time of January 1, 1970, that is, the timestamp.



How to format and output NSDate?

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

[FormattersetDateFormat: @ "MM dd, yyyy"];

NSDate * date = [formatter stringFromDate: detaildate];

NSLog (@ "date: % @", date );

Think: Create an NSDateFormatter class and pass an NSDate. "YyyyMMddHHMMss" indicates year, month, day, hour, minute, and second respectively.

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.