Date Picker format problems - iphone

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.

Related

Comparing two dates for daily reward in iOS game

I was implementing a 'daily reward' method in order to give some coins every day in my game.
I have a json that contains the actual date and time.
The issue is that I don't know how to compare the dates.
Here is my code -
#implementation ItemRewardManager
+(NSDate *)dateFromUTCTimeStamp:(NSString *)dateString{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *myDate = [df dateFromString: dateString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[df setTimeZone:gmt];
[[NSUserDefaults standardUserDefaults] setObject:myDate forKey:#"lastDatePlayed"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)getData
{
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:dateUrl]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSArray *keys = [jsonObjects allKeys];
// values in foreach loop
for (NSString *key in keys) {
NSLog(#"%# is %#",key, [jsonObjects objectForKey:key]);
}
}
+(void)dailyReward{
NSLog(#"llega al daily");
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"HH:mm:ss zzz"];
NSDate *now = [NSDate date];
NSString *dateString = [dateFormat stringFromDate:now];
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"lastDatePlayed"] isEqualToString: dateString])
{
NSLog(#"Actual date is the same as json date");
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:nil
message:#"You have win 5 coins! Come back tomorrow for more."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil] autorelease];
[alert show];
return;
}
}
#end
So, when I open my app and call this method, my app crashes for some reasone.
I think the problem might be on the comparison of the two dates.
Can someone tell me if there is something wrong?
Use following.
if ([date1 compare:date2]==NSOrderedSame)
date1 and date2 are NSdate objects.
NSDate has methods for comparing dates.
Here they are:
isEqualToDate: Returns a Boolean value that indicates whether a given
object is an NSDate object and exactly equal the receiver.
- (BOOL)isEqualToDate:(NSDate *)anotherDate
laterDate: Returns the later of the receiver and another given date.
- (NSDate *)laterDate:(NSDate *)anotherDate
earlierDate: Returns the earlier of the receiver and another given
date.
- (NSDate *)earlierDate:(NSDate *)anotherDate
Try like this. It may help you
NSDate *now = [NSDate date]; // current date
NSDate *newDate = [dateFormat dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:#"lastDatePlayed"]]; // server date
NSComparisonResult result;
result = [now compare:newDate]; // comparing two dates
if(result == NSOrderedAscending)
NSLog(#"now is less");
else if(result == NSOrderedDescending)
NSLog(#"newDate is less");
else
NSLog(#"Both dates are same");
Use this code.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:#"03/01/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/1440;
This is implemented code.

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

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

Changing of time zone

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

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