Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Hello Oliver, Thank you for your explanation and time. Now i have a question like: I have stored multiple dates in a database.And what i need was,how to get a time with minimum time difference with respect to current time.
I have 9:30 pm,8:30 pm and 7:30 am in my database. If the current time is 7:30 pm, 8:30 pm will be my desired result. What should i do??????
NSDate *myDate = [NSDate date]; //gets the current date & time [myDate addTimeInterval:5184000]; //adds 60 days worth of seconds (60 days x 24hours x 60 minutes x 60 seconds)
Alternatively, if you're just trying to set a date to something a long time in the future so that your initial boundary condition is met you can use....
What database??? If you're using some form of SQL you need to do something along the lines of...
SELECT TOP(1) * FROM datesTable ORDER BY ABS((dateValue - Now())) ASCENDING
Although the exact syntax will depend on what flavour of SQL you're using. Most databases store dates as a numerical value with the integer part representing the date and the fractional part representing time. It's therefore possible to perform basic arithmetic on them to find the difference.
The above query asks the database to calculate the difference between Now and the date value. It returns the Absolute value of this (so that it doesn't matter whether the matching time is earlier or latter than the current time) sorts them in ascending order (first record is closest) and then returns only the top 1.
nagarajan;83638 said:
Hello Oliver, Thank you for your explanation and time. Now i have a question like: I have stored multiple dates in a database.And what i need was,how to get a time with minimum time difference with respect to current time.
I have 9:30 pm,8:30 pm and 7:30 am in my database. If the current time is 7:30 pm, 8:30 pm will be my desired result. What should i do??????
Yeah, That works fine. Now I have needed a SQLite query that returns exactly a time which is very closest to the current time.
From 7:00pm,8:00pm, 9:00am and 10:00pm If the current time is 8:30pm, I will require 10:00pm.
Hope it clears my situation.Help me to get rid of this.
Best Regards, Nagarajan T.
playing around with SQL I came up with this.
Create a table for the demo:
CREATE TABLE dates (timestamp date); INSERT INTO \"dates\" VALUES('2009-05-29 19:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 20:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 21:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 22:00:00'); SELECT * from \"dates\";
With this setup I can now query for the row which has the least distance to now. If you just subtract dates then you get the number of years. So I first convert it to julianday-s which are "number of days since noon in Greenwich on November 24, 4714 B.C". Then I sort by distance from now descending and limit to just row.
select timestamp from dates order by (julianday(DATETIME('NOW')) - julianday(timestamp)) desc limit 1;
What's confusing for somebody new to SQLite is that you expect the row limiter to be called top like in T-SQL.
Now there is one catch: From your question it seems that you are looking for FUTURE dates only. But alas that is possible too, I'd pull the date arithmetic into it's own column so that we can reuse it to also only get negative values. There the future date is greater than now and now minus timestamp is negative.
select timestamp, (julianday(DATETIME('NOW')) - julianday(timestamp)) date_dist from dates where date_dist < 0 order by date_dist desc limit 1;
And for greater detail including how to query from within your app check out my article here.
regards
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
CREATE TABLE dates (timestamp date); INSERT INTO \"dates\" VALUES('2009-05-29 19:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 20:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 21:00:00'); INSERT INTO \"dates\" VALUES('2009-05-29 22:00:00'); SELECT * from \"dates\";
With this setup I can now query for the row which has the least distance to now. If you just subtract dates then you get the number of years. So I first convert it to julianday-s which are "number of days since noon in Greenwich on November 24, 4714 B.C". Then I sort by distance from now descending and limit to just row.
select timestamp from dates order by (julianday(DATETIME('NOW')) - julianday(timestamp)) desc limit 1;
What's confusing for somebody new to SQLite is that you expect the row limiter to be called top like in T-SQL.
Now there is one catch: From your question it seems that you are looking for FUTURE dates only. But alas that is possible too, I'd pull the date arithmetic into it's own column so that we can reuse it to also only get negative values. There the future date is greater than now and now minus timestamp is negative.
select timestamp, (julianday(DATETIME('NOW')) - julianday(timestamp)) date_dist from dates where date_dist < 0 order by date_dist desc limit 1;
And for greater detail including how to query from within your app check out my article here.
Great Thanks Oliver, I will check here and let you know.
Replies
I've elaborated my response here
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeI will try and let you know.
Regards,
Nagarajan T.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeOliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeThank you for your explanation and time.
Now i have a question like:
I have stored multiple dates in a database.And what i need was,how to get a time with minimum time difference with respect to current time.
I have 9:30 pm,8:30 pm and 7:30 am in my database.
If the current time is 7:30 pm, 8:30 pm will be my desired result.
What should i do??????
Best Regards,
Nagarajan T.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeNSDate *myDate = [NSDate date]; //gets the current date & time
[myDate addTimeInterval:5184000]; //adds 60 days worth of seconds (60 days x 24hours x 60 minutes x 60 seconds)
Alternatively, if you're just trying to set a date to something a long time in the future so that your initial boundary condition is met you can use....
NSDate *myDate = [NSDate distantFuture];
Hope that helps
Kenny
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeSELECT TOP(1) * FROM datesTable ORDER BY ABS((dateValue - Now())) ASCENDING
Although the exact syntax will depend on what flavour of SQL you're using. Most databases store dates as a numerical value with the integer part representing the date and the fractional part representing time. It's therefore possible to perform basic arithmetic on them to find the difference.
The above query asks the database to calculate the difference between Now and the date value. It returns the Absolute value of this (so that it doesn't matter whether the matching time is earlier or latter than the current time) sorts them in ascending order (first record is closest) and then returns only the top 1.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeLike: I have a table in SQLite looking like x and I want to retrieve y.
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeI have needed a SQLite query that returns a time which is very closest to the current time.
From 7:00pm,8:00pm and 9:00pm
If the current time is 8:30pm,
I will require 9:00pm.
Best Regards,
Nagarajan T.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeNow I have needed a SQLite query that returns exactly a time which is very closest to the current time.
From 7:00pm,8:00pm, 9:00am and 10:00pm
If the current time is 8:30pm,
I will require 10:00pm.
Hope it clears my situation.Help me to get rid of this.
Best Regards,
Nagarajan T.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesomeplaying around with SQL I came up with this.
Create a table for the demo:
With this setup I can now query for the row which has the least distance to now. If you just subtract dates then you get the number of years. So I first convert it to julianday-s which are "number of days since noon in Greenwich on November 24, 4714 B.C". Then I sort by distance from now descending and limit to just row.
What's confusing for somebody new to SQLite is that you expect the row limiter to be called top like in T-SQL.
Now there is one catch: From your question it seems that you are looking for FUTURE dates only. But alas that is possible too, I'd pull the date arithmetic into it's own column so that we can reuse it to also only get negative values. There the future date is greater than now and now minus timestamp is negative.
And for greater detail including how to query from within your app check out my article here.
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeI will check here and let you know.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeGreat thanks,
Nagarajan T.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeTook me an hour to put it all together, resolve syntax errors and publish both an article and the response here.
I have a reputation to maintain.
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeOliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome