On Jul 27, 2006, at 10:17 AM, [EMAIL PROTECTED] wrote:

From: [EMAIL PROTECTED]
Subject: NSTimestamp calculations?

Whats the easiest way to subtract NSTimestamps from each other?
The methods getMinutes() etc both from NSTimestamp and Date are unfortunately deprecated :-(


The getTime() method is your friend!

here are a few useful methods from my NSTimestampHelper bag-o-tricks...

    public static int deltaDays( NSTimestamp one, NSTimestamp two ) {
long diff = two.getTime() - one.getTime(); // diff is in milliseconds long roundingOffset = ( diff >= 0 ) ? 12 : -12; // if diff is positive, offset up, if negative, offset down // next, convert diff to hours, offset by .5 days to round the end result, divide by 24 and truncate. long days = ( ( diff / ( 1000 * 60 * 60 ) ) + roundingOffset ) / 24;
        return (int)days;
    }
        
    public static int deltaHours( NSTimestamp one, NSTimestamp two ) {
long diff = two.getTime() - one.getTime(); // diff is in milliseconds long roundingOffset = ( diff >= 0 ) ? 30 : -30; // if diff is positive, offset up, if negative, offset down // next, convert diff to minutes, offset by .5 hour to round the end result, divide by 60 and truncate. long hours = ( ( diff / ( 1000 * 60 ) ) + roundingOffset ) / 60;
        return (int)hours;
    }
        
public static int deltaMinutes( NSTimestamp one, NSTimestamp two ) { long diff = two.getTime() - one.getTime(); // diff is in milliseconds long roundingOffset = ( diff >= 0 ) ? 30 : -30; // if diff is positive, offset up, if negative, offset down // next, convert diff to seconds, offset by .5 minute to round the end result, divide by 60 and truncate.
        long minutes = ( ( diff / ( 1000 ) ) + roundingOffset ) / 60;
        return (int)minutes;
    }


Enjoy!!

Alex

__alex cone
        ceo  c o d e f a b  inc
        [EMAIL PROTECTED]
        212.465.8484 x101
        http://www.codefab.com

"The perversity of the universe tends towards a maximum."
    Entropy, expressed as Finagle's (Murphy's) 1st Law


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to