Convert NSString to NSDate and timeZone? - objective-c

Ive been racking my brains with no luck. Could someone please tell my how i would convert this string:
NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"date_time"];
2014-04-13T17:00:00+11:0
into a NSDate? and time format ?
how can i covert this date into time format ?
here is my code:
NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"date_time"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateTimeStr];
NSLog(#"date %#", date);
NSTimeInterval seconds = [date timeIntervalSinceReferenceDate];
in console
NSLog(#"seconds==> %f", seconds);
i have time in miliseconds then how can i take out minutes/hours/ ?

NSString *dateString = #"2014-04-13T17:00:00+11:00";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar]
components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];

Related

I have a method that takes timezone name as a parameter and gives the current date's day component. But it gives different incorrect results

Suppose i have two parameters
Australia/Sydney (GMT+10) offset 36000 &
Asia/Kolkata (IST) offset 19800
as timezones, it gives different values for the day component of current day itself. I am not able to figure out what am i doing wrong.. :( A little clue will be appreciated
-(NSInteger)getDayWithTimeZoneName:(NSString *)timeZoneName{
// Instantiate a timezone
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
//Instantiate a date formatter
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:timeZone];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *dateStr = [df stringFromDate:[NSDate date]];
NSDate *date = [df dateFromString:dateStr];
// NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:date];
NSDateComponents *components = [[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]components:NSDayCalendarUnit fromDate:date];
NSInteger day = [components day];
NSLog(#"Date component for timeZone %# is %ld", timeZoneName,(long)day);
return day;
}
NSFormatter isn't designed to convert or calculate dates:
NSString *dateStr = [df stringFromDate:[NSDate date]];
dateStr is the date in Sydney (2016-05-16 0:00).
NSDate *date = [df dateFromString:dateStr];
date is GMT, 10 hours behind Sydney (2016-05-15 14:00).
Use NSCalendar to calculate dates:
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]];
NSInteger day = [components day];

Convert date-based NSString to NSDateComponents

Is it possible to parse out date components from an NSString-based date? For instance, if I have an NSDateFormatter with yyyy-MM-dd HH:mm:ss.SSS ZZZ, how can I get an NSDateComponents object from this string directly?
Specifically, I'd like to preserve timezones, and differentiate this NSDateComponents object with one created from the yyyy-MM-dd format.
Not so directly, but with a few steps:
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS ZZZ"];
NSString *dateString = #"2016-03-15 12:00:00.000 +0800";
NSDate *date = [formatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitTimeZone fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSTimeZone *timeZone = [components timeZone];

How to convert Nstring to NSDate?

i want to convert nsstring to nsdate
i have string have this value
08:00:00
i want to convert it so i wrote this code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:_startTime];
NSLog(#"%#",startDate);
_startTime have this value = 08:00:00
but startDate after i display it , it had this value
2000-01-01 06:00:00 +0000
Update I wrote this code
NSString *dateString = _startTime;
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [[NSDate alloc] init];
// voila!
startDate = [formatter dateFromString:dateString];
NSLog(#"%#",startDate);
i want the output be 08:00:00
You can't have NSDate without a date !
if you want to access the time from an NSDate you'll need to use NSDateComponents (or NSTimeInterval).
To access the time from your NSDate using NSDateComponents :
NSString *startTime = #"08:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:startDate];
NSLog(#"%d:%d:%d", [components hour], [components minute], [components second]);
I don't get what's the purpose of changing the string _startTime, do you want to compare dates ?
Edit :
NSString *startTime = #"08:00:00";
NSString *endTime = #"23:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSDate *endDate = [formatter dateFromString:endTime];
NSTimeInterval difference = [endDate timeIntervalSinceDate:startDate];
NSInteger temp = difference;
NSDate* newDate = [[NSDate alloc] init];
for (int i = 0; (i < difference && [newDate compare:startDate] == NSOrderedDescending); ++i)
{
newDate = [startDate dateByAddingTimeInterval:(temp - i*60*10)];
NSLog(#"%#",newDate);
}
run this and use NSDateComponents to log what you want !
Use this code i think this one help you..
NSString *dateStr = #"08:00:00";
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:#"HH:mm:ss"];
NSDate* mydate = [datFormatter dateFromString:dateStr];
NSLog(#"date: %#", [datFormatter stringFromDate:mydate]);
i think this is what you really want...

how to convert localized Date String to NSDate

I have NSString *localized_Date = minutesSinceMidnightToLocalizedTime (time units)
I get this string as 4:30 am, 10.00pm
how do I convert this string to NSDate?
PLease let me know.
I know it's a bit long code, but I wanted to make it as readable as possible.
This code uses minutesSinceMidnight instead of the localized string.
NSDate *currentDate = [NSDate date];
NSDateComponents *currentDateComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:currentDate];
NSDateComponents *currentDateToMidnight = [[NSDateComponents alloc] init];
[currentDateToMidnight setHour:-[currentDateComponents hour]];
[currentDateToMidnight setMinute:-[currentDateComponents minute]];
[currentDateToMidnight setSecond:-[currentDateComponents second]];
NSDate *midnight = [calendar dateByAddingComponents:currentDateToMidnight toDate:currentDate options:0];
NSDateComponents *midnightToDesiredDate = [[NSDateComponents alloc] init];
[midnightToDesiredDate setMinute:minutesSinceMidnight];
NSDate *desiredDate = [calendar dateByAddingComponents:midnightToDesiredDate toDate:midnight options:0];
NSDateFormatter is your friend, use the formats that suits your date/time
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:[NSString stringWithFormat:#"dd MMM yyyy HH:mm"]];
NSDate *_date = [df dateFromString:string];
Johan
Try this code:
NSString *dateString = #"4:30 am";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormatter dateFromString:dateString];
Hope this will help you.

Create a NSDate with a user selected time in objective c

int hour = [[array objectAtIndex:0] intValue]; //hour = 17
int min = [[array objectAtIndex:1] intValue]; //min = 51
NSLog(#"hour value is %d",hour);
NSLog(#"min value is %d",min);
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
int year = [components year];
int month = [components month];
int day = [components day];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
[components setYear:year];
[components setMonth:month];
[components setDay:day];
[components setHour:hour];
[components setMinute:min];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *itemDate = [cal dateFromComponents:components];
NSLog(#"the date is %#",itemDate);
Output:
the date is 2012-11-03 01:00:00 +0000 . But i want the output to be 2012-11-03 17:51:00..how can i get it?What am i doing wrong?
It appears your issue is a simple matter of not realizing that the NSDate object will be created using your own current timezone but the NSLog will print out the NSDate description using UTC. Since you appear to be in PDT (it will be PST later this weekend when the clocks change in the USA), your NSLog should show the desired date/time but it will appear to be 7 hours (8 hours later this weekend) off.
To show your date in the local timezone, use an NSDateFormatter to convert the NSDate to an NSString.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"the date is %#", dateString);