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"];
Related
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
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.
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]];
I am able to fetch NSString representing the current date and time in PST using the following code.
//get current date and time
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:#"America/Los_Angeles"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
[dateFormatter setTimeZone:timeZone];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
NSDate *date = [NSDate date];
NSString *currentDateStr = [dateFormatter stringFromDate:date];
NSLog(#"currentDateStr %#",currentDateStr);
[dateFormatter release];
EDIT:
But the following code to fetch NSDate from NSString doesn't serve my purpose.
//get date from string
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSDate *dt = [[NSDate alloc]init];
dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
"dt" appears to be null.
Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.
NSDateFormatter also has dateFromString: function.
EDIT:
//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
Make use of
[dateFormatter dateFromString:urString];
In your case urString is currentDateStr
Use
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
NSLog(#"I am inside the datePickerValueChanged - - %#", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}
I am very new in iPhone. I have one issue. First Just check my code.
NSString *timeZone=[NSString stringWithFormat:#"%#",[[NSTimeZone systemTimeZone] abbreviation]];
NSLog(#"Print Time Zone :- '%#'",timeZone);
NSRange Timerange=[timeZone rangeOfString:#"+"];
NSString *time;
if(Timerange.location == NSNotFound)
{
Timerange=[timeZone rangeOfString:#"-"];
time=[timeZone substringFromIndex:Timerange.location];
NSLog(#"Time is :- '%#'",time);
time=[time stringByReplacingOccurrencesOfString:#"-" withString:#"M"];
}
else
{
Timerange=[timeZone rangeOfString:#"+"];
time=[timeZone substringFromIndex:Timerange.location];
NSLog(#"Time is :- '%#'",time);
time=[time stringByReplacingOccurrencesOfString:#"+" withString:#"P"];
}
Its working proper in iPhone Simulator 4.2 but it crashes in simulator 4.3 so what can be the issue?
Thanks
NSDate *date=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSString *dateOfGame =[formatter stringFromDate:date];
NSString *timeZone = [dateOfGame substringFromIndex:12];
NSLog(#"dateOfGame%#",timeZone);
try with this code
you might need coded like this
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"z"];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];