A collection of time conversion methods in iOS development

Source: Internet
Author: User
Tags current time dateformat

This article mainly introduces the iOS development in the time conversion method collection, the need for friends can refer to the

When developing an iOS program, sometimes you need to adjust the time format to the format that you want, and we can use the NSDateFormatter class to handle it.

For example:

Instantiate a NSDateFormatter object

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

Set the time format, where you can set the format you want

[Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];

The current time of the system can be obtained with [NSDate Date]

NSString *currentdatestr = [dateformatter stringfromdate:[nsdate Date]];

The output format is: 2010-10-27 10:22:13

NSLog (@ "%@", currentdatestr);

Don't forget to release the Alloc object after you do not use it

[Dateformatter release];

iOS converts timestamp to time string class method code

The code is as follows:

Turn the date timestamp into a time string

@paaram the time that date is used for the conversion

@param formatstring Time Format (YYYY-MM-DD HH:mm:ss)

@return NSString return character Furu (2012-8-8-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 mentioned is the entire content of this article, I hope you can enjoy.

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.