Generating Date Logic for UIPickerView - iphone

I am new to ios development.
I am making a custom datepicker using UIPickerView.
I have to show dates ranging from today to next 20 days & I am done with generating those dates & assigning them to an array.Those Dates have format: Oct 16,2013.
But my problem is I want to show Weekday(in 3 characters),Month & day i.e. Wed,Oct 16 on UIpickerView. And for one current week only Days are shown i.e today,tomorrow,Fri,Sat,Sun,Mon,Tue and for Next Week it would have Wed,Oct 23 and so on.
Thanks in advance! Any help would be greatly appreciated!

Assuming that you have a NSDate object called myDate for example, You need to format the dates using a date formater:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, LLL d"];
NSString *myString = [dateFormat stringFromDate:myDate];
You can find the formatters and what they mean here:
NSDateFormatter formatting
To get today:
NSdate *today = [NSDate date];
Tomorrow:
NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]]
After tomorrow:
NSDate *afterTomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:tomorrow]

Related

why date gets changed when i try to change formate? [duplicate]

This question already has an answer here:
NSDateFormatter not giving me correct
(1 answer)
Closed 9 years ago.
this is the code that i am using for changing date formate
NSLog(#"newBirthDates%#",_newwBirthDates);
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"MM/dd"];
NSDate *date1 =[NSDate date];
NSString *string =[Form stringFromDate:date1];
NSLog(#"string%#",string);
NSDate *todaydate =[Form dateFromString:string];
NSLog(#"todaydate%#",todaydate);
this is what i get as output
newBirthDates(
"05/22",
"07/11",
"10/07",
"02/20"
)
newBirthDates(
"05/22",
"07/11",
"10/07",
"02/20"
)
string03/18
todaydate1970-03-17 18:30:00 +0000
now my question is why 3/18 become 03/17?? why one day get decreases
The answer is simple - time zones.
Take a close look at what NSLog prints out
1970-03-17 18:30:00 +0000
By default, a NSDateFormatter is set to your local timezone. That means, if your time zone is +5:30 giving it a date "1970/18/3" results in 1970-03-18 00:00:00 +0530.
However, NSLog always prints dates in GMT (zero) time zone, adding/substracting the time zone difference (5 hours and 30 minutes).
Basically, there is nothing to fix, you just have to understand how NSLog works if you want to use it to check NSDate values.
Your Log is showing as per string value, eliminating all important timezone differences.
Log of NSDate shows you the time from GMT.
And both the values are correct.
The sole primitive method of NSDate, timeIntervalSinceReferenceDate,
provides the basis for all the other methods in the NSDate interface.
This method returns a time value relative to an absolute reference
date—the first instant of 1 January 2001, GMT.
You must read NSDate Documentation.
for getting correct date you can use this one,
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"MM/dd"];
[Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date1 =[NSDate date];
NSString *string =[Form stringFromDate:date1];
NSLog(#"string%#",string);
NSDate *todaydate =[Form dateFromString:string];
NSLog(#"todaydate%#",todaydate);
above code will give the correct date.
The Main thing is TimeZone : [Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
This is the Correct Code :
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[Form setDateFormat:#"MM/dd"];
NSDate *date =[NSDate date];
NSString *string =[Form stringFromDate:date];
NSDate *todaydate = [Form dateFromString:string];
NSLog(#"todaydate %#",todaydate);

iOS get Time from Date

I'm developing an application and I need to parse a Tweet timestamp that actually is a string like this:
"Mon, 17 Oct 2011 10:20:40 +0000"
However what I need it is only the time. So what I'm trying to get it is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *date = [dateFormatter dateFromString:timestamp];
but when I try to print the Date with NSLog the output is (null) and I don't understand why. What I need, if it is possible, is to create a date object only with the time. Indeed later on I need to compare different dates but I care only about the time. It is not important if the date are different because of the day, month or year, the important is that I can compare them with the "timeIntervalSinceDate" to get the difference in seconds.
Any help will be really appreciated.
Thanks
Ale
NSDATE is al full date object, it needs a date en time.
You can use NSDateFormatter to only display the time, but you will need to parse the full string to get the NSDate object.
Here some code you can use:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Mon, 11 Jul 2011 00:00:00 +0200
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"EN"] autorelease]];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [dateFormatter dateFromString: timestamp];
[dateFormatter release], dateFormatter = nil;
You need to set the local to make sure it read the timestamp in the correct language.
Also alloced the dateFormatter outside any loops and release it after you're done with the loop.
Look at NSDateComponents. You should be able to create a date from components. You'll have to parse the timestamp yourself, though (which should be easy — just split on the colon and grab the number from each component).

NSDate independent of timezone?

I have an app that displays a timetable of certain ferry trips.
If I travel to a different timezone - say 4 hours behind, a 10am ferry trip now shows up as 6am?
I know this has got to do with how dates are treated based on their timezones, but I can't work out how to change that behaviour.
At the moment here's how I am getting the date and displaying it on a UILabel:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
[self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:#"departureTime"]]];
[self.arrivalTime setText:[dateFormatter stringFromDate:[self.route objectForKey:#"arrivalTime"]]];
[dateFormatter release];
Thanks in advance for your help.
You'll need to store the timezone that the ferry ride is taking place in and format it for that timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *now = [NSDate date];
NSLog(#"now:%#", [dateFormatter stringFromDate:now]);
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)];
[dateFormatter setTimeZone:timeZone];
NSLog(#"adjusted for timezone: %#", [dateFormatter stringFromDate:now]);
Outputs:
2011-10-10 20:42:23.781 Craplet[2926:707] now:20:42
2011-10-10 20:42:23.782 Craplet[2926:707] adjusted for timezone: 16:42
You have seen NSDateFormatter's setTimeZone method, yes?
http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setTimeZone:
(b.t.w., I'd be amazed if there was a ferry that involved crossing four time zones; sounds like a cruise ship itinerary to me)
You can also use the NSDateComponents class as described by apple's reference:
If you need to create a date that is independent of timezone, you can store the date as an NSDateComponents object—as long as you store some reference to the corresponding calendar.
In iOS, NSDateComponents objects can contain a calendar, a timezone, and a date object. You can therefore store the calendar along with the components. If you use the date method of the NSDateComponents class to access the date, make sure that the associated timezone is up-to-date.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW1
Don't confuse an NSDate value with a formatted output like NSLog. NSDate is GMT, Apple's docs:
The sole primitive method of NSDate, timeIntervalSinceReferenceDate,
provides the basis for all the other methods in the NSDate interface.
This method returns a time value relative to an absolute reference
date—the first instant of 1 January 2001, GMT.
NSTimeInterval referenceInterval = [[dateFormatter dateFromString:#"1 January 2001 GMT"] timeIntervalSinceReferenceDate];
NSLog(#"referenceInterval: %f", referenceInterval);
NSTimeInterval estInterval = [[dateFormatter dateFromString:#"1 January 2001 EST"] timeIntervalSinceReferenceDate];
NSLog(#"estInterval: %f", estInterval);
Output:
referenceInterval: 0.000000
estInterval: 18000.000000
NSDate *currentDateTime = datePicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE,MM-dd-yyyy HH:mm:ss"];
NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
NSLog(#"%#", dateInStringFormated);

NSTimer countdown label

It will keep counting down and updating the current time. Anyone has any idea how to approach this problem? I don't see how the NStimer will recognize the date format like this:
20110803 23:59:59
Look up NSDateFormatter. You can specify a format string (-[setDateFormat:])that lets you convert to AND from NSDate and NSString.
When converting back to your countdown view, you may want to use NSDateComponents and NSCalendar to get the pieces you need for your countdown label (instead of NSDateFormatter). You could do something like:
NSDateComponents *countdown = [[NSCalendar currentCalendar]
components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
fromDate:[NSDate date]
toDate:expiration
options:0];
This will calculate all the unit differences for you (with respect to the device's currently configured calendar settings).
You can get the components back with a call like [countdown day] or [countdown hour].
You can get the date easily by using NSDateFormatter like this to get a NSDate object from your string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd' 'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:your_date_string];
You could convert the date from NSString to NSDate by using the NSDateformatter class.
NSString *dateString = #"20110803 23:59:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
Now, in order to count down, you use the NSTimer class to fire every one second, then you can get the difference in seconds between the current date and your datevariable by simply use timeIntervalSinceDate.
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:yourDate];
Now that you have the difference in seconds, you convert it to whatever format you wish to display.
I didn't drill down into this problem deeply, therefore sorry, if my answer is a bit unappropriate.
In this case I would use NSDate. Using NSDateFormatter I would convert text data of xml to NSDate. Changing the value of NSDate, when every tick of NSTimer, properly.
And a bit of code, 'cause the symbols of NSDateFormatter sometimes create mess:
NSString *xmlDateFormat = #"yyyyMMdd HH:mm:ss";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:xmlDateFormat];
NSDate *yourDate = [dateFormatter dateFromString:timeStringFromXml];
This is for reading from xml. For changing value of NSDate when tick you can see the doc of NSDate. And for display the value in label I would arithmetically calculate every value and show it with the help of casual NSString:
[NSString stringWithFormat:#"%dday %dh %dm %ds", days, hours, minutes, seconds];

How to compare just dates not the time?

How can I compare the dates only, not the time. I am using
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *tempDate = #"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(#"%#",dateString);
It logs this: 2012-02-01 18:30:00 +0000
NSDate *now = [NSDate date];//Current Date
NSLog(#"%#",now);
It logs this: 2011-04-04 14:49:45 +0000
I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.
Suppose I have to strings
date1 = 3-2-2011;
date2 = 4-5-2020;
I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.
Thanks!
Simplest way is to compare date by converting it into string.
Sample Code is as shown below:
//Current Date
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
//Other Date say date2 is of type NSDate again
NSString *date2String = [formatter stringFromDate:date2];
//Comparison of Two dates by its conversion into string as below
if([date2String isEqualToString:dateString])
{
//Your logic if dates are Equal
}
else if(![date2String isEqualToString:dateString])
{
//Your Logic if dates are Different
}
EDIT:
Checkout this link.
Comparing dates
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html
Hope This Helps You. :)
Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.
If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];