It looks like you're new here. If you want to get involved, click one of these buttons!

nagarajan
Posts: **124**

Thank you Experts,

I just want to get a date which is some 50 or 60 days away from current date.

How do i get??

Thanks,

Nagarajan T.

I just want to get a date which is some 50 or 60 days away from current date.

How do i get??

Thanks,

Nagarajan T.

Post edited by nagarajan on

## Replies

538@ @ @Use a gregorian calendar, create a date components object and use dateByAddingComponents instance method of NSCalendar.

I've elaborated my response here

Get 50% until March 9th with promo code "mldrobnik"

124Thank you for your quick response.

I will try and let you know.

Regards,

Nagarajan T.

538@ @ @Did you want to "let me know"? :-)

Get 50% until March 9th with promo code "mldrobnik"

124Hello 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??????

Best Regards,

Nagarajan T.

50NSDate *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

50SELECT 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.

538@ @ @Ask more precise questions.

Like: I have a table in SQLite looking like x and I want to retrieve y.

Get 50% until March 9th with promo code "mldrobnik"

124I 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.

124Yeah, 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.

538@ @ @playing 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.

Get 50% until March 9th with promo code "mldrobnik"

124Great Thanks Oliver,

I will check here and let you know.

124Great thanks,

Nagarajan T.

538@ @ @Of course it does. ;-)

Took 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.

Get 50% until March 9th with promo code "mldrobnik"

547538@ @ @And a GOLDEN SPOON it is, dear sir. ;-)

Get 50% until March 9th with promo code "mldrobnik"