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.