如何更有效地转换和显示此 NSDate?
How can I convert and display this NSDate more efficiently?
这是我的代码:
NSDateFormatter *df = [NSDateFormatter alloc]init];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [df dateFromString:self.todayRecord.datetime];
self.todayRecord.datetime 是一个类似于以下内容的字符串:
2016-02-15 12:33:00
我想显示相同的日期但不显示秒数,如下所示:
2016-02-15 12:33
我现在能想到的方法是制作另一个日期格式化程序,只格式化秒数。像这样:
df.dateFormat = @"yyyy-MM-dd HH:mm";
NSDate *date = [df dateFromString:self.todayRecord.datetime];
我这样做了,显示变成了(空)。
喜欢
//create the NSDateFormatter first
NSDateFormatter *df = [NSDateFormatter alloc]init];
// set the date format based on your String
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// convert the string to date
NSDate *date = [df dateFromString:self.todayRecord.datetime];
// set the final dateformat what the output do you need
df.dateFormat = @"yyyy-MM-dd HH:mm";
// in here you get final output on String
NSString *finalString = [df StringFromdate:date];
// in here you get final output on date
NSDate *FinalDate = [df dateFromString:finalString];
这是我的代码:
NSDateFormatter *df = [NSDateFormatter alloc]init];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [df dateFromString:self.todayRecord.datetime];
self.todayRecord.datetime 是一个类似于以下内容的字符串:
2016-02-15 12:33:00
我想显示相同的日期但不显示秒数,如下所示:
2016-02-15 12:33
我现在能想到的方法是制作另一个日期格式化程序,只格式化秒数。像这样:
df.dateFormat = @"yyyy-MM-dd HH:mm";
NSDate *date = [df dateFromString:self.todayRecord.datetime];
我这样做了,显示变成了(空)。
喜欢
//create the NSDateFormatter first
NSDateFormatter *df = [NSDateFormatter alloc]init];
// set the date format based on your String
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// convert the string to date
NSDate *date = [df dateFromString:self.todayRecord.datetime];
// set the final dateformat what the output do you need
df.dateFormat = @"yyyy-MM-dd HH:mm";
// in here you get final output on String
NSString *finalString = [df StringFromdate:date];
// in here you get final output on date
NSDate *FinalDate = [df dateFromString:finalString];