Time conversion methods in ios development

Source: Internet
Author: User

Time conversion methods in ios development

This article mainly introduces the time conversion methods in ios development. For more information, see

When developing iOS programs, you sometimes need to adjust the time format to the desired format. In this case, you can use the NSDateFormatter class for processing.

For example:

// Instantiate an NSDateFormatter object

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

// Set the time format, which can be set as needed

[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];

// Use [NSDate date] to obtain the current system time

NSString * currentDateStr = [dateFormatter stringFromDate: [NSDate date];

// Output format: 10:22:13

NSLog (@ "% @", currentDateStr );

// Do not forget to release unused objects after alloc

[DateFormatter release];

Method Code for converting a timestamp into a time string

The Code is as follows:

// Convert the date timestamp into a time string

// @ Paaram date refers to the conversion time.

// @ Param formatString time format (yyyy-MM-dd HH: mm: ss)

// @ Return NSString the return character, for example, (11:11:11)

+ (NSString *) getDateStringWithDate :( NSDate *) date

DateFormat :( NSString *) formatString

{

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

[DateFormat setDateFormat: formatString];

NSString * dateString = [dateFormat stringFromDate: date];

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

[DateFormat release];

Return dateString;

}

The above is all the content of this article. I hope you will like it.

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.