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.