how to get hour with date format - ios

I have a time format like this
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss"];
from this, I need only HH value.
How to get that value?

NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH";
NSString *datestringofHour= [timeFormatter stringFromDate: localDate];
Now in datestringofHour variable you will have hour value as a string.
Another one as follows which can give you hour,minute,seconds in integer value
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];

Just switch:
[df setDateFormat:#"HH:mm:ss"];
to:
[df setDateFormat:#"HH"];

Related

How to set maximum date in uidatepickerview as today 23:59?

How do I set maximum date in UIDatepickerView as today 23:59pm.
I tried like this
datePicker.maximumDate = [NSDate date];
But it is not working.
How can I do this?
You can do something like,
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *currentDate = [NSDate date];
NSString *currentDateStr = [[df stringFromDate:currentDate] stringByAppendingString:#" 23:59"];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *maxDate = [df dateFromString:currentDateStr];
NSLog(#"max date : %#",maxDate);
NSLog(#"max date str : %#",[df stringFromDate:maxDate]);
yourDatePicker.maximumDate = maxDate;
Update :
Another approach using NSDateComponents as suggested by #droppy,
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];
dateComponents.hour = 23;
dateComponents.minute = 59;
NSLog(#"%ld : %ld : %ld",(long)day,(long)month,(long)year);
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *resultDate = [gregorianCalendar dateFromComponents:dateComponents];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSLog(#"result date : %#",resultDate);
NSLog(#"result date string : %#",[df stringFromDate:resultDate]);
yourDatePicker.maximumDate = resultDate;

iOS set date to MM//DD/YY 00:00:00

I know there's a few questions already on this but I keep running into the same error regardless of what I do to my code. I am working in the simulator, I have location set to my long/lat. When I try to convert the current date to zero hours, minutes, seconds I always get this result:
2015-07-10 08:53:19.868 xxxxxxxxxxxxxxxxx[2184:7462272] Today at start of day:2015-07-10 04:00:00 +0000
Here's my code:
NSDate* today = [[NSDate alloc] init];
NSCalendar* currentCalendar = [NSCalendar currentCalendar];
NSDateComponents* dateComponents = [currentCalendar components:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear fromDate:today];
NSInteger thisMonth = [dateComponents month];
NSInteger thisDay = [dateComponents day];
NSInteger thisYear = [dateComponents year];
NSDateComponents* dayComponents = [[NSDateComponents alloc] init];
[dayComponents setDay:thisDay];
[dayComponents setMonth:thisMonth];
[dayComponents setYear:thisYear];
NSDate* todayAtMidnight = [currentCalendar dateFromComponents:dayComponents];
NSLog(#"Today at start of day:%#", todayAtMidnight);
Even if add
[dayComponents setHour:0]; the hour still comes out as 4.
What am I doing wrong?
To get the beginning of today you can use
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLog(#"date %#", [calendar dateFromComponents:components]);
Make sure you have the correct timezone
2015-07-10 04:00:00 +0000 is UTC, not your local timezone.
To display it in your local timezone, use an NSDateFormatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog([dateFormatter stringFromDate:todayAtMidnight]);
Use NSDateFormatter as follow because system timezone is UTC:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate * curDate = [formatter dateFromString:[NSDate date]];
Then you'll get 2015-07-10 00:00:00.

get islamic data and georgian date in specific format in ios

i want to get the current date in format like this 24 February 2014 . i want
also get Islamic date in the same format as 5 jumada al-awwal 1436
how can i do that i saw other thing but most care about integer components.
i wrote this code which just get components
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSLog(#"%#",components);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
i used this to Islamic date
NSCalendar *islamicCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDate *today = [NSDate date];
[islamicCalendar rangeOfUnit:NSDayCalendarUnit startDate:&today interval:NULL forDate:today];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:islamicCalendar];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
//english output
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString * islamicDateString = [dateFormatter stringFromDate:today];
NSLog(#"%#", islamicDateString);

setting time when formatting date using NSDateformatter

I have following code to convert string to date. Is it possible that it sets time as "00:00:00" and not the current time?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd"];
NSString *str = #"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];
This gives dt as "2014-08-08 15:20:00 +0000" because I did the operation at 15:20.
Edit: I am using this date to convert it to integer later to store it in database:
int t = [dt timeIntervalSince1970];
If you are displaying the date dt with NSLog you will see what the date description method provides. If you want to see the date in a specific way that suits you use NSDateFormatter to format the date.
Example:
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd"];
NSString *str = #"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];
NSDateFormatter *displayDateformat = [[NSDateFormatter alloc] init];
[displayDateformat setDateFormat:#"yyyy-MM-dd"];
NSString *displayDateString = [displayDateformat stringFromDate:dt];
NSLog(#"displayDateString: %#", displayDateString);
Output:
2014-08-08
Note per Apple docs: "This method returns a time value relative to an absolute reference dateā€”the first instant of 1 January 2001, GMT."
A good practice is to use NSDateComponents
NSDate *yourDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:yourDate];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
// Set the time components manually
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
yourDate = [calendar dateFromComponents:dateComponents];
Update
iOS8 :
[[NSCalendar currentCalendar] startOfDayForDate:[NSDate date]];

Update Countdown Timestamp According to Timezone

Hi I have a countdown timer in my app and I am looking for a way to take my timestamp date and modify it based on the time zone without formatting it. The time stamp is GMT and it looks like this:
theDate = [NSDate dateWithTimeIntervalSince1970:1387929601];
Then I have my countdown formatter here:
-(void)updateCountdownText
{
//Update the Countdown Label
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:theDate options:0];
[dateLabel setText:[NSString stringWithFormat:#"%d%c %d%c %d%c %d%c", [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];
}
Since I'm already formatting the timestamp below all i would like to do is take the GMT timestamp number and convert it according to the local time zone. So I would just like to add the time zone code before the dateWithTimeInterval and then change that number (keeping it a timestamp) according to the time zone. Is this possible? Thanks!
Try this one:
//Timestamp convert to NSDate
double time=[myString doubleValue];//in yourcase mystring=#"1387929601"; mystring is timestamp
NSTimeInterval interval =time ;
NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a dd-MMM-yyyy"];
NSLog(#"Timestamp date is: %#", [dateFormatter stringFromDate:online]);
NSString *mystr=[dateFormatter stringFromDate:online];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [dateFormatter dateFromString:mystr];
value1=([[NSTimeZone localTimeZone] secondsFromGMT]);
value1=value1/3600;
float value123=((value1) * 3600);
NSTimeZone *tz = [NSTimeZone timeZoneForSecondsFromGMT:(value123)]; // for PST
NSDateComponents *dc = [cal components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:now];
[cal setTimeZone:tz];
NSDate *newDate = [cal dateFromComponents:dc];
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:#"hh:mm a dd-MMM-yyyy"];
NSLog(#"result: %#", [dateFormatter1 stringFromDate:newDate]);
May it will work for you.
happy coding...
Use NSDateFormatter to get correct time zone of your theDate object.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone localTimeZone];
NSTimeZone has a property secondsFromGMT. So you can use [NSTimeZone systemTimeZone].secondsFromGMT; to obtain the difference, add it to your time interval and instantiate the date.
Format the NSDate to adjust for time zone:
NSTimeZone *tz = [[NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];
[dateFormatter setTimeZone:tz];
NSString* s = [dateFormatter stringFromDate:now];

Resources