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];
Related
In my application, i want to raise the notification using selected day and time, not with date.So i have to show the day and time only in DatePickerView. Would you please help me in this problem. Thanks in advance.
You should use NSDateFormatter for this purpose.
For example:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// e.g. "Tue 16:53"
[dateFormatter setDateFormat:#"EEEE HH:mm"];
NSString *szDate = [dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"Date formatted: %#", szDate);
(remove release if ARC is enabled);
And btw, the questions of this kind were asked million times around stackoverflow
I am using below code to get date and time from datepicker.
When i am using formatter it is giving correct time .
but when i am converting that string into NSdate object ,then it is not giving the correct value of time.
NSDate *choice = [datepicker date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSString *dateString = [format stringFromDate:choice];
NSDate *date = [format dateFromString:dateString]; NSLog(#"%#",date);
DateString is giving correct value but when i am converting it in to NSdate again it is giving wrong value.
If the data that is being logged has some hour in difference then its ok since logging an NSDate will log it in GMT time zone, then this is not a bug, if your dateString is correct and your NSdate has some hours of difference then you have no issues
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];
I'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...
I think there might be a method in NSCalender? If there's such a method, please tell me.
Thanks in advance.
I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls:
NSDate * myDate = [NSDate date];
If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.
I have some code posted here:
How get a datetime column in SQLite with Objective C
that shows how to create NSDates from NSStrings using NSDateFormatter.
If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.
For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:#"2011-01-26 00:00"];
// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(#"date: %#", midnight_26_jan_2011_utc);
[formatter release];
Edit: Added time to format string.
You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:
NSString* defaultDateString = [NSString stringWithFormat:#"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];
and if you wanted to get the string from a date you can just use:
NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need
NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];
Now, you can use the string the date. :)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:#"2011-01-26"];
I have this string...
2010-08-24T16:00:00-05:00
and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];
Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?
Thanks!
The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?
I think you should be able to do something like the following.
// create the date formatter object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [formatter dateFromString:dateString];
// set up the new date format
[formatter setDateFormat:#"hh:mm:ss"];
NSString *twelveHourTime = [formatter stringFromDate:date];
[formatter release];
Update: Fixed the dateFormatter string format. I had the line below, but the Z seems to be unnecessary. Timezones always screw me up. :-/
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This answer needs to be updated. As of iOS 10 the system provided NSISO8601DateFormatter is available for this particular format.