Objective c stringWithFormat的问题:@“%02dd.%02dh.%02dm.%02ds”
我想格式化如下的日期和时间: 2014年7月22日上午8:30 我将非常感激在这件事上能得到的一切帮助Objective c stringWithFormat的问题:@“%02dd.%02dh.%02dm.%02ds”,objective-c,nsdateformatter,nscalendar,gregorian-calendar,stringwithformat,Objective C,Nsdateformatter,Nscalendar,Gregorian Calendar,Stringwithformat,我想格式化如下的日期和时间: 2014年7月22日上午8:30 我将非常感激在这件事上能得到的一切帮助 - (NSString *)getRemainingTimeFromNowToDateThreeComponents:(NSString*)parseDate withFormat:(NSString*)format { NSString *retTime = @""; NSDateFormatter *dateFormatter = [[NSDateFormatter al
- (NSString *)getRemainingTimeFromNowToDateThreeComponents:(NSString*)parseDate withFormat:(NSString*)format {
NSString *retTime = @"";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *endDate = [dateFormatter dateFromString:parseDate];
NSDateComponents *comps = [gregorian components:NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date] toDate:endDate options:0];
if ([comps day] > 0) {
retTime = [NSString stringWithFormat:@"%02dd.%02dh.%02dm.%02ds", [comps day], [comps hour], [comps minute], [comps second]];
} else if ([comps hour] > 0 || [comps minute] > 0 || [comps second] > 0) {
retTime = [NSString stringWithFormat:@"%02dh.%02dm.%02ds", [comps hour], [comps minute], [comps second]];
} else {
retTime = @"00h.00m.00s";
}
return retTime;
}
好的。。。我添加了EN-POSIX
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
我可以选择上午/下午没有问题。但,我仍在努力显示上述日期和时间
2014年7月22日星期三上午8:30
以下是我所做的:
if(alertView == alert_succ)
{
if(buttonIndex == 0)
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSString *str = [NSString stringWithFormat:@"%d-%d-%d %@:%@", components.month, components.day, components.year, [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy h:mm:ss a"];
NSDate *alertTime = [formatter dateFromString:str];
//NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:60];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc]init];
if(notification)
{
notification.fireDate = alertTime;
NSString *str = [NSString stringWithFormat:@"%@ %@ %@ \n %@ %@ %@:%@", NSLocalizedString(@"Mirror_List", nil),
NSLocalizedString(@"for", nil), self.category.description, self.site.Name, NSLocalizedString(@"at", nil),[dateArray objectAtIndex:0], [dateArray objectAtIndex:1]];
notification.alertBody = str;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:tick forKey:@"Booking"];
[dic setObject:self.category.description forKey:@"name"];
[notification setUserInfo:dic];
notification.alertAction = NSLocalizedString(@"Details", nil);
...
}
这是我的dateam/pm问题的最后一部分
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm-dd-yyyy hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]);
使用此格式打印日期您应该标记语言,因为它使查找更容易,但也会影响颜色语法突出显示。上述格式类似于10d.08h.30m.45s,因为这是您要求的格式。使用NSDateFormatter会更简单。我这样做了,但当我在设备上从24小时切换到12小时时,它仍然显示24小时的值。您需要MM,而不是当月的MM。并且您需要使用en_US_POSIX的特殊区域设置来避免24/12小时问题。如果不是针对。