I am creating a cricket app. I saved the following data in sqlite:
matchno
country1
flag1
country2
flag2
date
time
venue
This data is displayed in a UITableView.
My requirement is how to display the list of the next day's matches (example today Eng vs Ban). I want display tomorrow's match (Ind vs Ireland) in my tableview.
I tried to compare today's date and tomorrow's date, and how to write query.
My query is
select * from where date = 'Mon Mar 9'
It displays the list of today's matches, but I want to display the list of tomorrows matches.
Plz help me.
//get tomorrow date
NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *nextDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *tomorrowDate = [formatter stringFromDate:nextDate];
after this fire query
NSString *query=[[NSString alloc]initWithFormat:#"select * from ICCWorldCupMatchesList WHERE date = %#",tomorrowDate];
may help you.
Related
I have some data which contains sold date. I want to sort them according to Week, Month, Year. For eg. If I select week then results returned should be within 7 days of current date. Same goes for Month and Year.
The relevant field from the data which is fetched from a web service looks like the following:
AuctionStartTime = "05/03/2016 09:30:00 AM"
You have use NSDateFormatter to convert the string to your date format and using NSDateFormat to get required date values and perform your task. The set date format using following code...
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
Convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The your date format to set your required format. For more details of date format to click now.
You shoud use a NSDateFormatter to convert the NSString date to NSDate then you can compare that to current date. After that you can easily pick the last week's, month's or year's dates.
Background: I have dates stored in files.
What I would like to do: I would like to take the difference between two dates in seconds. I can't find any way to do it. My date format looks like that:
2015-23-02-12-23-43
Try this out:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd-hh-mm-ss"];
NSDate *date = [dateFormatter dateFromString:string1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
Try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:EnterYourStringHere];
NSString *str = [dateFormatter stringFromDate:date];
If all you need is seconds then use
NSTimeInterval dateInterval = [date timeIntervalSince1970];
Then
NSString secondsString = [NSString stringWithFormat: #"%.0f", dateInterval];
EDIT:
If you have a file full of date strings that look like your example, 2015-23-02-12-23, then you could use code like this:
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"yyyy-mm-dd-hh-mm-ss"];
//an example - you'll read from your file
NSString *aDateString = #"2015-23-02-12-23";
NSDate aDate = [myFormatter dateFromString: aDateString];
NSTimeInterval dateSeconds = [aDate timeIntervalSince1970];
That will give you dateSeconds as a double precision floating point number, which is the norm for numeric date calculations since it deals with fractions of seconds. You can then do numeric comparisons of date's time intervals.
Note that most UNIX systems use the numeric values returned by the timeIntervalSince1970 method, but Mac and iOS uses numeric date values returned by the method timeIntervalSinceReferenceDate. The two methods have different "epoch dates", or dates where they start counting from zero.
timeIntervalSince1970 uses midnight on Jan 1, 1970 (GMT)
timeIntervalSinceReferenceDate uses midnight on Jan 1, 2001 (GMT)
Which you use is up to you, just be sure to use the same method in all cases.
By the way, your date string format is horrible. having all those 2 digit numbers separated by dashes doesn't give the reader any way to tell apart month/day/hours/minutes/seconds. It's all a jumble. You'd be much better off using a standard date format.
In the US it's common to display dates in the form mm/dd/yyyyy (or yyyy/mm/dd, or even yyyy/dd/mm), and times as hh:mm:ss, so in "mm/dd/yyyyy hh:mm:ss" format you'd get:"09/02/2015 13:09:39" (I'm using human-readable date format strings for discussion, not those intended to set up a date formatter.)
Can someone explain why NSDateFormatter return same strings from different dates:
NSDateFormatter *f = [NSDateFormatter new];
f.dateFormat = #"MM/dd/yyyy";
NSDate *dateYear0 = [[NSCalendar currentCalendar] dateWithEra:0 year:0 month:3 day:31 hour:0 minute:0 second:0 nanosecond:0];
NSDate *dateYear1 = [[NSCalendar currentCalendar] dateWithEra:0 year:1 month:3 day:31 hour:0 minute:0 second:0 nanosecond:0];
NSString *dateStrYear0 = [f stringFromDate:dateYear0];
NSString *dateStrYear1 = [f stringFromDate:dateYear1];
NSLog(#"dateYear0=%# \t\t dateStrYear0=%#",dateYear0,dateStrYear0);
NSLog(#"dateYear1=%# \t\t dateStrYear1=%#",dateYear1,dateStrYear1);
Both dateStr1 and dateStr2 have the same values:
NSLog result:
dateYear0=0001-03-30 21:57:56 +0000 dateStrYear0=03/31/0001
dateYear1=0000-03-30 21:57:56 +0000 dateStrYear1=03/31/0001
Its looks like 0 year affect this somehow?
Thanks
That's because year 0 doesn't exist in Gregorian Calendar. When dealing with AD dates minimum possible value for NSDate is "0001-01-01 00:00:00 +0000" therefore it uses "0001" as year whenever a lower value used than 1. You can check minimum NSDate value with distantPast class method.
Also if you want to use BC dates you should check Apple's guideline for more information.
In my iPhone / iPad app I am showing a UIDatePicker for time. It will display time in this format, 11:00 AM. When User clicks on the time row we expand the time row to display this datePicker row.
I have a time stamp in string "starts": "11:00", // time of day in ISO-8601 format
I need to show this on the picker wheel as selected time when it gets opened up. For this, first of all I get the date at 12 AM using https://stackoverflow.com/a/9040554/4082792. Then I convert the given time (11:00) to number of seconds and add it to the midnight time to get the current time. This time is in local timezone (as I specify the timezone while using NSDateFormatter). When I try to setDate to UIDatePicker with this date, It gives me incorrect time, even though the time is correct in the NSDate variable. For 11:00 AM, it gives me 6:40 while the local time is 4:30.
So, I have two questions :
1) Why is the time wrong on wheel.
2) How can I convert the NSDate from one timezone to another, I need to show it in the local time format.
Snippet :
NSString *strDate = #"11:00";
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:date]];
///Start time
NSString *startTime = #"11:00";
NSArray *startTimeSeparatedByColon = [startTime componentsSeparatedByString:#":"]; /// From 22:10 to [22, 10];
NSInteger hourPartOfStart = startTimeSeparatedByColon[0] ? [startTimeSeparatedByColon[0] integerValue] : 0;
NSInteger minutePartOfStart = startTimeSeparatedByColon[1] ? [startTimeSeparatedByColon[1] integerValue] : 0;
NSTimeInterval totalTime = (hourPartOfStart*60*60+minutePartOfStart*60);
NSDate *finalDate = [NSDate dateWithTimeInterval:totalTime sinceDate:date];
NSDate *dt = [NSDate dateWithTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT sinceDate:finalDate];
self.datePicker.date = dt;
By default, iOS converts date into the device's time zone.
But if you want to convert date into another time zone, here is the code for that:
NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:currentDateTimeZone];
You can get the date in "EST" time zone from this dateFormatter object.
Convert "EDT" TimeZone
NSString *format = #"EEEE dd-MMMM-yyyy HH:mm:ss z";
NSDateFormatter *date_EDTDateFormate = [[NSDateFormatter alloc] init];
date_EDTDateFormate setDateFormat:format];
date_EDTDateFormate setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EDT"]];
NSString *stringEDT = [dateFormatter_EDT stringFromDate:date_System];
I am taking date of string having format yyyy-MM-DD from database then converting it into NSDate having format yyyy-MM-DD. And again converting back it into string format dd-MM.
But when at last I get date in string format it shows one month before date.
Here is the code I have used:
NSDateFormatter *format=[[NSDateFormatter alloc]init];
NSDateFormatter *format2=[[NSDateFormatter alloc]init];
NSString *date;
[format setDateFormat:#"yyyy-MM-DD"];
[format2 setDateFormat:#"DD-MMM"];
dict=[[NSMutableDictionary alloc]init];
for(int i=0;i<[viewHistoryData count];i++)
{
dict=[viewHistoryData objectAtIndex:i];
date=[dict objectForKey:#"Date"];
NSLog(#"My date with out format = %#",date);
NSString *dateString =[format2 stringFromDate:[format dateFromString:date]];
NSLog(#"My date is = %#",dateString);
[tempArray addObject:dateString];
}
OUTPUT
2014-02-07 15:01:07.586 VirtualRunner-V3[3580:c07] My date with out format = 2014-02-07
2014-02-07 15:01:07.588 VirtualRunner-V3[3580:c07] My date is = 07-Jan
Does anybody know how to solve this?
You are using the wrong placehodler for day of month. What you are using is Day of Year ranging from 1 ... 365. So instead of
[format setDateFormat:#"yyyy-MM-DD"];
use
[format setDateFormat:#"yyyy-MM-dd"];
See this link for a complete overview.
The format DD representes day of the year, not day of the month, so change your formatters to this:
[format setDateFormat:#"yyyy-MM-dd"];
[format2 setDateFormat:#"dd-MMM"];
Correct the two format.
[format setDateFormat:#"yyyy-MM-dd"];
[format2 setDateFormat:#"dd-MMM"];
But I think, Issue related with timezone. Use below to format.
[format2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
See this link