如何将特定日期转换为 am/pm 格式?
How to convert the particular date to the am/pm format?
我从字符串中得到了一个特定的日期
NSString *str = @"2016-05-04 08:42:00 +0000";
我想将其转换为 08:42 AM 格式。
这是我的代码
NSString *str = @"2016-05-04 08:42:00 +0000";// put here item.TimeStart
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"EEE, dd MMM YYYY HH:mm:ss VVVV"];// here give the format which you get in TimeStart
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"hh:mm a"];
NSString *convertedString = [dateFormatter stringFromDate:date1];
NSLog(@"Converted String : %@",convertedString);
但这在转换后的字符串中返回 nil。为什么 ??有什么想法吗??
请添加以下行将时区时差设置为0,您将获得实际时间。
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
添加后,您的最终代码将如下所示:
NSString *str = @"2016-05-04 08:42:00 +0000";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"YYYY-dd-MM HH:mm:ss VVVV"];
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init];
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter1 setDateFormat:@"hh:mm a"];
NSString *convertedString = [dateFormatter1 stringFromDate:date1];
NSLog(@"Converted String : %@",convertedString);
它会帮助你
-(NSDate *)calculateDate:(NSString *)getString
{
NSDateFormatter *Format = [[NSDateFormatter alloc] init];
[Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *aDate = [Format dateFromString:getString];
if( !aDate )
{
[Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss a"];
[Format setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
aDate = [Format dateFromString:getString];
}
return aDate;
}
当我们有一个像2001-02-15T13:25:00这样的字符串,我们想把它格式化成12H格式,我们只需要定义local。
我从字符串中得到了一个特定的日期 NSString *str = @"2016-05-04 08:42:00 +0000"; 我想将其转换为 08:42 AM 格式。 这是我的代码
NSString *str = @"2016-05-04 08:42:00 +0000";// put here item.TimeStart
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"EEE, dd MMM YYYY HH:mm:ss VVVV"];// here give the format which you get in TimeStart
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"hh:mm a"];
NSString *convertedString = [dateFormatter stringFromDate:date1];
NSLog(@"Converted String : %@",convertedString);
但这在转换后的字符串中返回 nil。为什么 ??有什么想法吗??
请添加以下行将时区时差设置为0,您将获得实际时间。
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
添加后,您的最终代码将如下所示:
NSString *str = @"2016-05-04 08:42:00 +0000";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:@"YYYY-dd-MM HH:mm:ss VVVV"];
NSDate *date1 = [dateFormatter1 dateFromString: str];
dateFormatter1 = [[NSDateFormatter alloc] init];
//Add this line to set the time zone time difference to 0
dateFormatter1.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter1 setDateFormat:@"hh:mm a"];
NSString *convertedString = [dateFormatter1 stringFromDate:date1];
NSLog(@"Converted String : %@",convertedString);
它会帮助你
-(NSDate *)calculateDate:(NSString *)getString
{
NSDateFormatter *Format = [[NSDateFormatter alloc] init];
[Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *aDate = [Format dateFromString:getString];
if( !aDate )
{
[Format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss a"];
[Format setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
aDate = [Format dateFromString:getString];
}
return aDate;
}
当我们有一个像2001-02-15T13:25:00这样的字符串,我们想把它格式化成12H格式,我们只需要定义local。