Advertise here




Advertise here

Howdy, Stranger!

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

Sign In with Google Sign In with OpenID
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.

NSDate

nagarajannagarajan Posts: 124Registered Users
edited May 2009 in iPhone SDK Development
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.
Post edited by nagarajan on

Replies

  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    nagarajan;81658 said:
    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.
    Use a gregorian calendar, create a date components object and use dateByAddingComponents instance method of NSCalendar.

    I've elaborated my response here
    regards



    Oliver Drobnik

    Cocoanetics - Our DNA is programmed in Objective-C.



    Linguan – makes
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    Oliver Drobnik;81677 said:
    Use a gregorian calendar, create a date components object and use dateByAddingComponents instance method of NSCalendar.

    I've elaborated my response here
    Thank you for your quick response.
    I will try and let you know.

    Regards,
    Nagarajan T.
  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    nagarajan;81773 said:
    Thank you for your quick response.
    I will try and let you know.

    Regards,
    Nagarajan T.
    Did you want to "let me know"? :-)
    regards



    Oliver Drobnik

    Cocoanetics - Our DNA is programmed in Objective-C.



    Linguan – makes
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    Oliver Drobnik;82897 said:
    Did you want to "let me know"? :-)
    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??????

    Best Regards,
    Nagarajan T.
  • kendolondonkendolondon Posts: 50Registered Users
    edited May 2009
    To get a date exactly 60 days in the future....

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

    NSDate *myDate = [NSDate distantFuture];

    Hope that helps
    Kenny
  • kendolondonkendolondon Posts: 50Registered Users
    edited May 2009
    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??????

    Best Regards,
    Nagarajan T.
  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    nagarajan;83638 said:

    What should i do??????
    Ask more precise questions.

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



    Oliver Drobnik

    Cocoanetics - Our DNA is programmed in Objective-C.



    Linguan – makes
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    Thank you for your quick responses.

    I 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.
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    Oliver Drobnik;81677 said:
    Use a gregorian calendar, create a date components object and use dateByAddingComponents instance method of NSCalendar.

    I've elaborated my response here
    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.
  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    nagarajan;84369 said:
    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.



    Linguan – makes
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    Oliver Drobnik;84370 said:
    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.
    Great Thanks Oliver,
    I will check here and let you know.
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    That works fine Oliver.

    Great thanks,
    Nagarajan T.
  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    nagarajan;84561 said:
    That works fine Oliver.

    Great thanks,
    Nagarajan T.
    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.
    regards



    Oliver Drobnik

    Cocoanetics - Our DNA is programmed in Objective-C.



    Linguan – makes
  • sindhutiwarisindhutiwari Posts: 547Registered Users
    edited May 2009
    @above .. from all the posts i understood ..what is spoon feeding ..:D
    In order to succeed, your desire for success has to be greater than your fear for failure
  • Oliver DrobnikOliver Drobnik Posts: 537New Users, Registered Users
    edited May 2009
    sindhutiwari;84627 said:
    @above .. from all the posts i understood ..what is spoon feeding ..:D
    And a GOLDEN SPOON it is, dear sir. ;-)
    regards



    Oliver Drobnik

    Cocoanetics - Our DNA is programmed in Objective-C.



    Linguan – makes
Sign In or Register to comment.