-2

I have two strings.

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

I need to save these as NSDates, in the format , so that it will be 2012-05-21 13:00:00 +0000.

My approach so far has been:

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];

When I debug the code, startTime prints as 2012-05-29 07:30:00 +0000 which is incorrect (both the day and time are incorrect). I think this is because of the GMT time.

I need the date and time to be 2012-05-21 13:00:00 +0000.

jscs
  • 63,694
  • 13
  • 151
  • 195
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
  • I have the `Date` and `Time` saved as Strings.I need to use them and make a NSDate object. The example you provided doesn't help as it just postpones the time by a few hours. – Sharon Watinsan May 29 '12 at 05:01
  • No i don't want to diplay todays date. I need to display the Date given in the string NSString *dateof = @"Mon May 21"; and the time NSString *timeof = @"01.00 pm from 11.50 pm";. I need to create a NSDate object with the Date as Mon May 21 and the time as 01.00 pm. – Sharon Watinsan May 29 '12 at 05:25
  • 1
    I believe that my solution is what you need. Please let me know. – lnafziger May 31 '12 at 20:18
  • @sharon: would you look at the answer from lnafziger on this old question to see if that might have helped? Please consider interacting with it in some way (voting, accepting, commenting), since someone took the time and effort to help you. – halfer Nov 28 '17 at 12:47

2 Answers2

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

Output:

str=2012-05-29 at 10:36

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitin
  • 7,455
  • 2
  • 32
  • 51
  • I want the output not be as a String. it should be NSDate. – Sharon Watinsan May 29 '12 at 04:57
  • Could you help me by editing my code. because i have no clue how to integrate your code to solve my problem. help – Sharon Watinsan May 29 '12 at 05:04
  • No i don't want to diplay todays date. I need to display the Date given in the string `NSString *dateof = @"Mon May 21";` and the time `NSString *timeof = @"01.00 pm from 11.50 pm";`. I need to create a NSDate object with the Date as `Mon May 21` and the time as `01.00 pm`. – Sharon Watinsan May 29 '12 at 05:13
0

This will do it. As far as the local/GMT, this will give you a NSDate that contains the local time but the debugger will show always show it in GMT. You will need to format it (using a NSDateFormatter) into the format that you want if you are going to show it to the user, in which case it will be in local time.

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];
lnafziger
  • 25,760
  • 8
  • 60
  • 101