Changing of time zone - iphone

I'm doing objective-c (iOS) development. May I know how do I go about changing the default time zone of GMT +0 to GMT +8. GMT +8 is for Singapore.
Here is my code:
- (IBAction)dateChanged {
NSDate *choice = [datePick date];
NSString *words = [NSString alloc initWithFormat:#"%#", choice];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You have selected:" message:words delegate:nil cancelButoonTitle:#"OK" otherBUttonTitles:nil, nil];
[alert show];
[alert release];
[words release];
}
Thanks.

You can use,
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
OR,
+ (id)timeZoneForSecondsFromGMT:(NSInteger)seconds
Which will be in your case,
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*60*60]];

Related

adding events to default calendar in iPhone

i am trying to add the event to iPhone default calendar and the code is as follows
NSMutableString *startDateString = [NSMutableString stringWithString:#"11/20/2012 10:00 AM"];
NSMutableString *endtDateString = [NSMutableString stringWithString:#"11/20/2012 5:00 PM"];
NSDateFormatter *dateFormatter = [[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
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm a"];
NSDate * startDate = [[NSDate alloc]init];
startDate = [dateFormatter dateFromString:startDateString];
NSDate *endDate = [[NSDate alloc]init];
endDate = [dateFormatter dateFromString:endtDateString];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"TEST";
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[startDate release];
[endDate release];
if(!err)
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"TEST" message:#"Event Added successfully " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
NSLog(#"error is %#",err);
}
the event is successfully adding but the problem is its not adding to event start date its just adding to jan 1st 2001 date all the events are adding to the same date only .
can any one please help me where i am getting wrong & i am using the iPhone 4s & iPhone 3gs with 5.1.1 version.
Thanks in advance .
Set DateFormat to dateFormatter like this:
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
Note: yourdateString should be according to dateFormatter

EKCalender creates calendar but can't see it in iCal

I am trying to create a new calendar in iCal. Now I am able to create a new Calendar with some name. But my problem is when I try to create to Local calendar I cant see it in calendar list. Any one have any idea about this? I have tried using all types of sources but not able to find exact solution for this.
Thank you in advance.
NSMutableArray *arrAlarm = [[NSMutableArray alloc]init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:3600];
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-3600]; //86400 1 day
[arrAlarm addObject:alarm1];
[arrAlarm addObject:alarm2];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *ekevent = [EKEvent eventWithEventStore:eventStore];
ekevent.title = strname;
NSLog(#"%#",ekevent.startDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM d,yyyy"];
NSDate *date = [dateFormatter dateFromString:strdate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS ±HHMM"];
NSString *newDate = [dateFormatter stringFromDate:date];
// dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
// [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate* firstDate = [dateFormatter dateFromString:time];
NSDate* secondDate = [dateFormatter dateFromString:endtime];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:MM"];
NSString *t11 = [dateFormatter stringFromDate:firstDate];
NSString *t12 = [dateFormatter stringFromDate:secondDate];
NSDate *dt1 = [dateFormatter dateFromString:t11];
NSDate *dt2 = [dateFormatter dateFromString:t12];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh"];
NSString *hourstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df setDateFormat:#"mm"];
NSString *minstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: [hourstring intValue]];
[components setMinute: [minstring intValue]];
[components setSecond: 00];
// NSDate *newDate = [gregorian dateFromComponents: components];
[gregorian release];
ekevent.startDate = date;
ekevent.endDate = [[NSDate alloc] initWithTimeInterval:timeDifference sinceDate:date];
[ekevent setCalendar:[eventStore defaultCalendarForNewEvents]];
ekevent.alarms = arrAlarm;
[arrAlarm release];
NSError *err;
[eventStore saveEvent:ekevent span:EKSpanThisEvent error:&err];
Try this code you will get the desired result.

Date Picker format problems

How do i solve the problem where i got 2011-07-21 15:55:01 +0000 , but the timing is off by 4 hr, how do i remove the +0000? And I want the format to be Eg. Friday 12 June 2011 1:30 PM
-(IBAction)addButton:(id)sender
{
NSDate *choice = [datepick date];
NSString *words = [[NSString alloc]initWithFormat:#"%#", choice];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Date Chosen
message:words
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[words release];
label.text = words;
textfield.text = words;
}
- (void)viewDidLoad
{
NSDate *now = [[NSDate alloc] init];
[datepick setDate:now animated:YES];
[now release];
datepick.minimumDate = [NSDate date];
[super viewDidLoad];
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE dd MMMM yyyy h:mm a"];
NSString *words = [formatter stringFromDate:choise];
Here date formatter guide.

String to NSDate

I am trying to convert string to date. have used the following code. But getting wrong date.
Thanks,
NSString *dateString = [NSString stringWithFormat:#"12-07-2011"];
//NSString *dateString = [NSString stringWithFormat:#"%d-%d-%d", selectedDate.day, selectedDate.month, selectedDate.year];
NSLog(#"%#",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSDate* d = [dateFormatter dateFromString:dateString];
NSLog(#"%#",d);  
 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Selected Date" message: [NSString stringWithFormat:#"%#",d] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
The problem is you are using the minute and not the month. M
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
Unicode Date Format Patterns
Use:
[dateFormatter setDateFormat:#"dd-MM-yyyy"];

How to specify a time zone in UIDatePicker

- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];
NSString *message = [[NSString alloc] initWithFormat:#"The date and time you selected is: %#", selected];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Date and Time Selected" message:message delegate:nil cancelButtonTitle:#"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
So my question is next... How can I specify a time zone using a Datepicker in Objective C?
now if I choose some values from DatePicker and press the button which displays an alert - the time is wrong. It's probably because of incorrect time zone but I'm not sure. Well any ideas would be great. On the picture below you can see that I chose 2:14 PM (14:14) but an alter says that it is 11:14 and the time zone is +000
UPDATE 1
I have added a few changes to my code but still nothing...
- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:tz];
NSString *formattedDate = [dateFormatter stringFromDate:selected];
NSString *message = [[NSString alloc] initWithFormat:#"The date and time you selected is: %#", formattedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Date and Time Selected"
message:message delegate:nil cancelButtonTitle:#"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[dateFormatter release];
Use this at the point where use defined your datepicker
datePicker.timeZone = [NSTimeZone localTimeZone];
You can set the date with timezone
Objective C
datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:10*60*60]; // Like GMT+10
in Swift 3:
datePicker.timeZone = TimeZone(secondsFromGMT: 5*60*60) // Like GMT+5
in Swift 3:
datePicker.timeZone = NSTimeZone.local
You can set the date with timezone strings like Australia/West (+08:00) , WST, Asia/Kolkata (+05:30) , IST
datePicker.timeZone = [NSTimeZone timeZoneWithName:#"timezone string goes here"] ;
I think this is because of the date format being picked, use an NSDateFormatter to specify the appropriate date format and use it in your string, here is a link Link