0

I would like to format a date and time like this:

07-22-2014 08:30am

I will appreciate all the help I can get on this.

- (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;
}

OKAY... I added the EN-POSIX

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

and I am able to select am/pm with no issue. BUT, I am still struggling with the displaying the date and time as described above.

Wednesday 07-22-2014 08:30am

Below is what I did:

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);
            ...
        }

This is the final part of the issues with date(am/pm) for me.

4

1 回答 1

0
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      [formatter setDateFormat:@"mm-dd-yyyy hh:mm a"];
       NSLog(@"Current Date: %@", [formatter stringFromDate:date1]);  

使用此格式打印您的日期

于 2014-07-22T15:39:25.063 回答