There is UIDatePicker in my app. I have set it's mode as "Time".
In it's default selected row current time showing. So how to change it?
I want to display 9:00 AM as default selected, does't matter what is current time.
I have tried with below code,
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *date = [formatter dateFromString:#"9:00 a"];
[myDatePicker setDate:date];
but, it's getting crashed with "Invalid parameter not satisfying: date"
Can anybody help me out here?
Thanks in advance!
There seems to be some kind of problems with setting up the properties in the interface builder but at this link you can find everything you need:
Default Date for UIDatePicker in iOS
The Mode of Date Picker is "Time".
Add two more property to get the default custom value.
Set Date property as "Custom"
In the below test box, provide a default value which the picker needs to display. (As Time property is selected for date picker, while providing default value - dates can be ignored)
Related
I am developing an application for OS-X. I want to take time as input from user. I have placed three text fields like below
But If I take input in this way How am I going to convert it into date&Time ? Because I want to compare this with system time.
How can I do that please advice ?
Concate all UITextField data i.e
NSString * strTime = [NSString stringWithFormat:#"%#:%# %#",txtHours.text, txtMin.text, txtAMPM.text];
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"HH:mm a"];
NSDate *startD = [dateFormatter1 dateFromString:strTime]; // this code is used for converting string into nsdate
Now, you can compare current system time with this above NSDate.
Hope this will help you :)
my UIDatepicker returns a wrong value and I don´t understand why. See picture.
It shows 0 hours and 1 min but the label shows -1 hour.
I don´t find an time zone to set up. I expected that my app uses the time zone of the machine it´s on.
My action to fill the label:
- (IBAction)datePickerDateChanged:(id)sender {
[_timerOutput setText:[NSString stringWithFormat:#"%#", [_timePicker date]]];
}
I´m on a German system and I set the UIDatepicker to German but it still shows hours and mins.
Any idea what I´m doing wrong?
Ronald
This is the date and time in GMT wintertime (== UTC). use a NSDateFormatter to adjust it, it should use the default timezone by default. you also can set another.
try
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *myDateString = [dateFormatter stringFromDate: [_timePicker date]];
NSLog(#"%#", myDateString);
Some theory:
A NSDate object does not represent what we call a date in every-days sense, like a day or a time of range, or what ever. It represents a single point in time. with sub-millisecond precision. And it does so by counting time intervals. From the beginning of a certain era. and by definition it is keeping UTC timezone as reference.
The internal counter will always be counting in UTC-context, it is your responsibility to display it correctly. But the NSDateFormatter is of huge help for that.
BTW: That your displayed time is a UTC time formatted one is also shown by +0000, as it has 00 hours and 00 minutes timezone offset from UTC.
So I am using an array of dates as Section Headers in my UITableView. The dates of my data in the arrays is as follows
07/12/2012
07/13/2012
07/14/2012
But when I run the app the section headers are all moved back one day so they are as follows:
07/11/2012
07/12/2012
07/13/2012
What gives? The data I am pulling from the server is specific to timeZone. We know when our app will be used and on what day (think traveling circus).
I am sure this has to do with NSTimeZone, so I tried the following, which did not work.
self.sectionDateFormatter = [[NSDateFormatter alloc] init];
[self.sectionDateFormatter setDateStyle:NSDateFormatterLongStyle];
[self.sectionDateFormatter setTimeStyle:NSDateFormatterNoStyle];
[self.sectionDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
Anyone know have a solution?
Set the dates to UTC and it fixed the issue.
This question already has answers here:
Get NSDate from NSDate adjusted with timezone
(2 answers)
Closed 9 years ago.
I am trying to use NSDateFormatter in my app which takes a date string and formats it to an NSDate so that I can do Date Comparisons, however I am finding when I use dateFromString and format it the date is losing one day.
NSString *dateString = #"02-06-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"My Date = %#", dateFromString);
[dateFormatter release];
This outputs to the console:
My Date = 2012-06-01 23:00:00 +0000
Try adding this lines to your code,
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
or
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
SWIFT update :
Code from quetion,
let dateString = "02-06-2012"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var dateFromString : NSDate = dateFormatter.dateFromString(dateString)!
println("My Date \(dateFromString)")
And Solution ,
dateFormatter.timeZone = NSTimeZone(name: "GMT")
OR
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT+0:00")
I don't believe that Dhruv's answer is correct. In fact, it's not clear there's any problem at all. You just seem to have an incorrect expectation of what should happen and/or interpretation of what's happening.
NSDate represents a moment in time. This moment does not have one unique name. It will be known by different names in different places and under different naming systems (time zones, calendars). NSDate doesn't deal with any of this, except lamely in its -description method, where it has to produce a string representation of that moment.
Second, a string like "02-06-2012" doesn't specify a precise moment in time. First of all, it's just a date with no time information, so NSDateFormatter just defaults to the first moment for that date. Second, it doesn't specify the time zone. The first moment of the calendar day is a different moment in each time zone. Unless you specify a time zone with -setTimeZone: or the string itself carries time zone information, NSDateFormatter assumes that any date strings you ask it to parse are in the current time zone.
So, your dateFromString object represents the first moment of the specified date, 02-06-2012, in your time zone. I expect this is what you wanted. However, you then got confused by the way that NSDate describes itself when logged. As I said, NSDate has to pick some "name" (string representation) for the moment it represents and which name it picks is fairly arbitrary. These days it is picking the name that the moment is known by in UTC. I gather from the log output shown in your question that you are located at UTC+0100. So, the date may look like it's one day earlier but it really is the same moment you specified. In other words, "2012-06-01 23:00:00 +0000" and "2012-06-02 00:00:00 +0100" are two equivalent names for exactly the same moment in time. You just aren't used to seeing the first one and misinterpreted it.
The lesson is that you have to stop relying on NSDate's self-description to be in any particular time zone. Really, you have to not rely on anything about it, since it's not documented. In fact, the docs for -[NSDate description] state, "The representation is not guaranteed to remain constant across different releases of the operating system."
Dhruv's solution seems to help merely because it causes NSDateFormatter and -[NSDate description] to agree on the time zone. But that's unreliable. It wouldn't work on Snow Leopard, for example, because -[NSDate description] used the local time zone instead of UTC in that version of the frameworks.
More importantly, though, it alters the actual moment represented by the NSDate object you get from NSDateFormatter's interpretation of your date string. I suspect you really want that to have a specific meaning – you want the string to be interpreted as being in the local time zone – and his solution thwarts your intent.
tl;dr: you were getting the date you wanted all along; don't rely on -[NSDate description]; don't use Dhruv's solution
How can I customize a UIDatePicker to only show the month and year? There's an existing mode that shows the day, month, and year, but I'd like to get rid of the day portion.
Simply put, you can't. UIDatePicker does not have any API for only showing years and months.
You therefore have 3 possible recourses:
Make your own out of a UIPickerView
Use a UIDatePicker in Date mode and just ignore the day portion
File a bug asking for this capability and hope it gets added at some point
I vote for #1 and #3.
It is not Possible to customize the date picker like you need in the nib file.You can set the mode in nib to get only date or time or both.In this case you need to format your date to the required format using dateFormatter.For this you can set the format to "yyyy-MMM",which will returns you the year and month.
eg:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MMM"];
NSString *formattedDate = [dateFormatter stringFromDate:date];
[dateFormatter release];