-2

我有两个字符串。

NSString *dateof = @"Mon May 21";
NSString *timeof = @"01.00 pm from 11.50 pm"; // 01.00 pm is the start time and 11.50 pm is the end time

我需要将这些保存为NSDates,格式为2012-05-21 13:00:00 +0000.

到目前为止,我的方法是:

NSDate *currentTime        = [NSDate date];
NSCalendar *cal            = [NSCalendar currentCalendar];
NSDateComponents 
*currentDateComps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
                           fromDate:currentTime];

NSString *startDateStringWithYear = [NSString stringWithFormat:@"%@ %d", startDateString, currentDateComps.year];
NSString *endDateStringWithYear   = [NSString stringWithFormat:@"%@ %d", endDateString, currentDateComps.year];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MM dd yyyy"];

[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *startDate = [formatter dateFromString:startDateStringWithYear];

NSArray *timeDurationStringWords = [timeof componentsSeparatedByString:@" "];

// Calculate NSDate's for the start time for today:
NSString *startTimeString = [timeDurationStringWords objectAtIndex:0];
currentDateComps.hour     = [[startTimeString substringToIndex:2] intValue];
currentDateComps.minute   = [[startTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:1] isEqualToString:@"pm"]) 
{
    currentDateComps.hour += 12;
}
NSDate *startTime         = [cal dateFromComponents:currentDateComps];

当我调试代码时,startTime打印出2012-05-29 07:30:00 +0000哪个不正确(日期和时间都不正确)。我认为这是因为格林威治标准时间。

我需要日期和时间2012-05-21 13:00:00 +0000

4

2 回答 2

0

这会做到的。至于本地/格林威治标准时间,这将为您提供一个包含本地时间的 NSDate,但调试器将始终以格林威治标准时间显示。如果要将其显示给用户,则需要将其(使用 a NSDateFormatter)格式化为您想要的格式,在这种情况下它将是本地时间。

NSString *dateof = @"Mon May 21";
NSString *timeof = @"01.00 pm from 11.50 pm";
NSDate   *today  = [NSDate date];

// Start time will be array index 0, end time will be index 1:
NSArray  *times  = [timeof componentsSeparatedByString:@" from "];

NSCalendar *cal  = [NSCalendar currentCalendar];
NSDateComponents *yearComp = [cal components:NSYearCalendarUnit fromDate:today];

// Append the year and the time to the dateof string to get a string like @"Mon May 21 2012 01.00 pm":
NSString *start  = [dateof stringByAppendingFormat:@" %d %@", yearComp.year, [times objectAtIndex:0]];
NSString *end    = [dateof stringByAppendingFormat:@" %d %@", yearComp.year, [times objectAtIndex:1]];

// Make a date formatter that recognizes the above date/time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM dd yyyy hh.mm a"];

// Convert the strings to dates:
NSDate *startDate = [formatter dateFromString:start];
NSDate *endDate   = [formatter dateFromString:end];
于 2012-05-30T02:39:58.447 回答
0
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSLog(@"str=%@",str);
NSDate *dt = [NSDate date];
dt = [formatter dateFromString:str];
NSLog(@"dt=%@",dt);

输出:

str=2012-05-29 在 10:36

dt=2012-05-29 05:06:00 +0000

于 2012-05-29T04:54:38.907 回答