When developing iOS programs, sometimes we need to adjust the time format to the format we want. At this time, we can use the NSDateFormatter class to handle it.
For example:
//Instantiate an NSDateFormatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the time format, here you can set it to the format you need
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//Use [NSDate date] to get the current time of the system
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//The output format is: 2010-10-27 10:22:13
NSLog(@"%@",currentDateStr);
//Don't forget to release the object that is not used after alloc
[dateFormatter release];
ios method code for converting timestamps into time string classes
//Convert date timestamp into time string
//@paaramdate
//@paramformatString Time format (yyyy-MM-dd HH:mm:ss)
//@returnNSString Return character characters such as (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 is the entire content of this article, I hope you like it.