please see the following code
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
NSDate *dat=pickerView.date;
NSString *timerString=[outputFormatter stringFromDate:dat];
[startTimer setTitle:timerString forState:UIControlStateNormal];
timeString=timerString;
[self updatePrayerTimigs];
//dat=[NSDate dateWithTimeIntervalSinceNow:20];
UILocalNotification *local=[[[UILocalNotification alloc]init] autorelease];
UIApplication *app=[UIApplication sharedApplication];
if(local){
local.fireDate=dat;
local.timeZone=[NSTimeZone defaultTimeZone];
//local.repeatInterval=0;
local.alertBody=#"Hello";
local.repeatInterval=NSMinuteCalendarUnit;
[app scheduleLocalNotification:local];
[app presentLocalNotificationNow:local];
}// end of the if
I am selecting only time (Hour , minute) from UIDatePicker so i can fire notifications, so I decided to add current date to above NSDate with selected from UIDatePicker , please help how can I do that
Try this to format the date
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Related
Hello I'm Beginner in ios I Have create Custom UIDatePicker View This show right date and time but when we click on done button then this show date right but time is not right ....So how do this ....
UIDatePicker *DatePicker;
UITexField *DateTime;
if ([[UIScreen mainScreen] bounds].size.height >480)
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,350,320,50)];
}
else
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,270,320,50)];
}
DatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;
DatePicker.datePickerMode = UIDatePickerModeDateAndTime;
DatePicker.date = [NSDate date];
[self.DateTime setInputView:DatePicker];
DatePicker.userInteractionEnabled=YES;
[self.view addSubview:DatePicker];
UIDatePicker *picker = (UIDatePicker*)self.DateTime.inputView;
self.DateTime.text = [NSString stringWithFormat:#"%#",picker.date];
NSLog(#"date picker is %#",self.DateTime.text);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss +HHmm"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%#",DateTime.text]];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:#"AM"];
[dateFormatter setPMSymbol:#"PM"];
[anotherDateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSLog(#"%#", [anotherDateFormatter stringFromDate:myDate]);
DateTime.text =[anotherDateFormatter stringFromDate:myDate];
NSLog(#"datetime is %#",DateTime.text);
this picker showh right time and date in UI but when we click done button(not mention here)then it show .... DateTime.text=date picker is 2013-07-03 05:22:57 +0000 after this format it shows 2013-07-03 12:00:57 +0000
03/07/2013 12:00 AM
datetime is 03/07/2013 12:00 AM
When we Select any time then this show only above time ....Date is right ...but time is wrong so ....solve this problem
this code may be help you
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm"];
strDatetime = [formatter stringFromDate:picker.date];
[datetimeLbl setText:strDatetime];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"HH:mm"]; //24hr time format
NSString *dateString = [outputFormatter stringFromDate: picker.date];
NSLog(#"%#",dateString);
It will print the proper time.
This may be an issue of NSTimeZone.
You can try this
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
Can someone tells me whats wrong with my time formatter? when i used ipod 6.0 the time formatter works. but when i used iphone 5 6.1 the time formatter returns nil.
Here are my codes regarding to the timeFormatter.
NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtime = #"2013-05-27 19:00";
//set end date and time
NSDateFormatter *tempFormatterTo = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatterTo setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtimeTo = #"2013-05-27 20:00";
NSDate *startDate = [tempFormatter dateFromString:dateandtime];
NSDate *endDate = [tempFormatterTo dateFromString:dateandtimeTo];
Thanks in advance. please help me.
Try to use this format. HH used for 24 hour format.
[tempFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
Try This
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate* d = [df dateFromString:#"2013-05-27 19:00"];
NSLog(#"%#", d);
here problem in your Format change it to yyyy-MM-dd HH:mm also use my bellow method when you want to convert any NSString date to NSDate .. This is my custom method just paste this method in your .m class..
-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:dateFormat];// set format here which format in string date
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
and use above method like bellow...
NSDate *startDate = [self convertStringToDate:#"2013-05-27 19:00" dateFormate:#"yyyy-MM-dd HH:mm"];
NSDate *endDate = [self convertStringToDate:#"2013-05-27 20:00" dateFormate:#"yyyy-MM-dd HH:mm"];
I am creating an app where the current date needs to be displayed in a label and automatically updated each day. I have looked at a few tutorials and they have all shown how to get the date in YYYY:DD:MM hh:mm, I am wondering how to change this format to DD/MMM/YYYY.
Any sample code or links to tutorials would be greatly appreciated.
Something like this:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateformatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
[myLabel setText:[formatter stringFromDate:today]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate: dayDate];
//display dateString in label
[dateFormatter release];
You need to use the NSDateFormatter. For specfic formats look at
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
Always i'm struggling with NSDate in iphone apps. I have developed one iphone app for US based client. The app should show the todays date. I have used the following code in my app. The date is showing correctly for me(Am in India). But, the date is also showing the india date for my client in (US). I want to show the date from the iphone/ipad device.
NSDate *today1 = [[NSDate alloc] init];
NSString *dates = [NSString stringWithFormat:#"%#",[today1 description]];
dates = [dates substringToIndex:10];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterBehaviorDefault];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dates];
How to do this? Any suggestions/sample code to solve this? I has spend 6 hours to solve my problem but, i didn't find the answer? Please help me. Thanks to spend your valuable time with me.
Use Locale
See code blow Taken from here
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSString *dateFormat;
NSString *dateComponents = #"yMMMMd";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
Updated answer below
I have tested this should work out of the box any where is the world.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"Date: %#:", dateString);
Try using
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
I use date picker and I set its mode to be
pickerView.datePickerMode = UIDatePickerModeDate ;
when select august 3o, 2011
to display It I use
NSDate * selected = [pickerView date];
NSString * date = [selected description];
DateLabel.text = date;
the output is 2011-08-29 21:00:00:00 + 0000
any suggestion on how to display the correct date only
Have a look at the NSDateFormatter class.
try using:
NSDate *selected = [pickerView date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:kCFDateFormatterNoStyle];
[formatter setDateStyle: kCFDateFormatterShortStyle];
NSString *date = [formatter stringFromDate:selected];
[formatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"DD/MM/yyyy"];//you can change format here #"MM/yyyy"
DateText.text = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter release];