Advertise here




Advertise here

Howdy, Stranger!

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

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 Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    nagarajan wrote: »
    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
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    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 Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    nagarajan wrote: »
    Thank you for your quick response.
    I will try and let you know.

    Regards,
    Nagarajan T.

    Did you want to "let me know"? :-)
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    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 wrote: »
    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 Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    nagarajan wrote: »
    What should i do??????

    Ask more precise questions.

    Like: I have a table in SQLite looking like x and I want to retrieve y.
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
  • 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
    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 Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    nagarajan wrote: »
    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.
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
  • nagarajannagarajan Posts: 124Registered Users
    edited May 2009
    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 Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    nagarajan wrote: »
    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.
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
  • sindhutiwarisindhutiwari Posts: 547Registered Users
    edited May 2009
    @above .. from all the posts i understood ..what is spoon feeding ..:D
    <font face="Trebuchet MS">In order to succeed, your desire for success has to be greater than your fear for failure </font>
  • Oliver DrobnikOliver Drobnik Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited May 2009
    @above .. from all the posts i understood ..what is spoon feeding ..:D

    And a GOLDEN SPOON it is, dear sir. ;-)
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
Sign In or Register to comment.