Problem in getting time - iphone

HI i need to increment 15 minutes in time intrval which i get in the webservice .
The code is as follows
NSString *dateString =obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *strTime = [dateFormatter1 stringFromDate:scheduledForYellow];
[dateFormatter1 release];
NSArray *aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
Here to add 15 minutes to my start time i have written the above code , but in end time that is [aryy objectAtIndex:1] does not give me the correct time.What i wanted to do is increment 15 minutes for whatever date i get in start date .Dont know whats the issue is.
`

It must be because of the default timezone (local timezone) that the date formatter uses to generate the string. I have modified your code a bit to reuse the date formatter and fixed a leak with dateFromString.
NSString *dateString = obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate * dateFromString = [dateFormatter dateFromString:dateString];
NSDate * scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString * strTime = [dateFormatter dateFromString:scheduledForYellow];
NSArray * aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
This should fix the issue. Let me know if you face any issues.

Related

Retrieving hours in 24 hour format in iphone

I am using this format to get hours
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
The problem is it returns e.g 4Pm i Want it to return 16 Instead of 4. How can I do that? I tried replacing hh by single h by of no avail.
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
[dateFormatter setDateFormat:#"HH"];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
Here is answer :-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
NSInteger myHour = [date intValue];
NSLog(#"%d",myHour);
Hope it helps you!
Its for 12 hour time format :-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
Its for 24 hour time format:-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);

How to get the localized day of an NSDate as string?

I have an NSDate object and must create a label that indicates the day of the week in a short 3-char style like 'Mon', 'Tue', 'Wed', ... and localized to the short representation in any language.
The date formatter EEE has 3 chars but how does that translate to languages like Chinese which probably only have one character?
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE"];
NSString *strDate = [formatter stringFromDate:date];
[formatter release];
//Commented out the following line as weekday can be of one letter (requirement changed)
//strDate = [strDate substringToIndex:3]; //This way Monday = Mon, Tuesday = Tue
To use the correct locale you'll want to set the dateFormat on the instance of NSDateFormatter using the + dateFormatFromTemplate:options:locale: method.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:#"EEE" options:0 locale:[NSLocale currentLocale]];
NSLog(#"Weekday using %#: %#", [[NSLocale currentLocale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Test what Chinese will look like without changing device locale in settings
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"zh_CN"];
NSLog(#"Weekday using zh_CN: %#", [dateFormatter stringFromDate:date]);
This prints:
Weekday using en_US: Thu
Weekday using zh_CN: 周四
This will print what you need:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
NSString *day = [formatter stringFromDate:date];
[formatter release];
if ([day length] > 3) day = [day substringToIndex:3];
NSLog(#"%#", day);

ios: String to Date conversion

I am trying to convert my date from NSString to NSDate using following code
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
currMonth = 3;
currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);
Above code is giving me wrong date. Can anyone please help?
What is your output? Keep in mind, that NSDate is in UTC.
2012-03-01 00:00:00 (GMT+1) is 2012-02-39 23:00:00 (UTC)
Another tip:
%02 formats your integers with leading zeros.
Try this:
-(NSDate *)dateFromString:(NSString *)string
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:string];
date1 = [date1 dateByAddingTimeInterval:interval];
if(!date1) date1= [NSDate date];
[dateFormat release];
[locale release];
return date1;
}
Tell me if it helps u :]
try
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
int currMonth = 3;
int currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
//SET YOUT TIMEZONE HERE
dateformatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);

Converting NSString to NSDate with date and time being received as different string

I know this have been asked a lot but I am still having the problem in this conversion even after extensive search on the net. Can some one point me to my mistake in following code?
NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:#"%# %#",myEvent.startDate,myEvent.timeFrom];
NSLog(#"eventDttm: %#",eventDttm);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM a"];
//conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:eventDttm];
[formatter release];
NSLog(#"%#",dateFromString);
I am getting date and time separately in string as following:
myEvent.startDate= 2011-11-22
myEvent.timeFrom= 08:00 PM
but dateFromString is always null
Don't mix Months and minutes (MM & mm)
[formatter setDateFormat:#"yyyy-MM-dd HH:mm a"];

Wrong Date from string returns from formatter! Why?

I have problem with NSDateFormatter
I converting date from picker in one view
`NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
[output setDateStyle:NSDateFormatterShortStyle];
[output setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *StringToSend = [output stringFromDate:datePicker.date];
`
then send string to other nib
where converting it back with that code
`NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateStyle:NSDateFormatterMediumStyle];
[inputFormatter setDateStyle:NSDateFormatterShortStyle];
[inputFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:StringFromOtherView];
NSLog(#"%#", formatterDate);`
and it's return wrong date
sending 2011-09-26 01:02:49
geting 2011-09-25 22:02:49 +0000
what is wrong?
Because:
When you NSLog() an NSDate, it always logs it in GMT
You live in a timezone that's three hours ahead of of GMT. Thus, 1:02 am for you is 22:02 pm (of the previous day) in GMT.
Include the timezone on your date format string.
+ (NSDate*) dateFromString:(NSString*)aStr
{
if (aStr.length == 10) {
aStr = [aStr stringByAppendingFormat:#" 00:00:00"];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSLog(#"strdate %#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
NSLog(#"date = %#",aDate);
[dateFormatter release];
return aDate;
}