我想像这样格式化日期和时间:
07-22-2014 08:30 am
我将非常感谢能得到的所有帮助。
- (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"]];
我可以选择上午/下午没有问题。但是,如上所述,我仍然在努力显示日期和时间。
星期三07-22-2014 08:30 am / p>
以下是我的所作所为:
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);
...
}
这是我的日期(上午/下午)问题的最后一部分。
答案 0 :(得分:0)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm-dd-yyyy hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]);
使用此格式打印日期