Day of Year with iOS [duplicate] - cocoa

This question already has answers here:
How do you calculate the day of the year for a specific date in Objective-C?
(6 answers)
Closed 9 years ago.
I need to know day of year with iOS sdk. I can do that manually, but wanted to know that is there any method to find day of year? I am not meaning that - How many days in a year. I need to know the number of day for a input date?

Got answer :) please see the below example
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
NSLog(#"%d",dc);

Related

Xpath/xQuery difference in months

I am trying to get the difference between two dates in months for an xPath.
I have no problems getting it in days (1127)
days-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
When I try doing it in months I get 0
months-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
I did notice that this comes back as days only ("P1126D") so that maybe the problem just not sure how to fix it.
xs:date('2012-06-17')-xs:date('2009-05-17')
Thanks for any help!
So the best thing I can seem to do is manually calculate it.
(year-from-date(xs:date('2012-06-17')) - year-from-date(xs:date('2012-05-18')))*12 + month-from-date(xs:date('2012-06-17')) -month-from-date(xs:date('2012-05-18')) + (if (day-from-date(xs:date('2012-06-17')) < day-from-date(xs:date('2012-05-18')) ) then -1 else 0)
This function was not included in the spec because we couldn't agree semantics for it. We heard arguments that the difference between 31 March 2015 and 30 Sept 2015 was six months, and arguments that it was five months. (Advice: negative differences seem to be even more problematic than positive differences.) You can easily find that a date plus 6 months minus 6 months is not the date where you started. You'll have to define what you think the answer should be, and implement it using lower-level facilities such as month-from-date and year-from-date.
The problem is that days-from-duration returns the days field -- not the number of days the duration includes. Likewise for months and years.
Now, you can divide by days, but not by months (since months have uneven boundaries):
(: arg1 here is a dayTimeDuration, which can't be easily converted to a yearMonthDuration :)
let $arg1 := xs:date('2012-06-17')-xs:date('2009-05-17')
return xs:dayTimeDuration($arg1) div xs:dayTimeDuration("P30D")

How do I obtain the sub-second portion of an NSDate?

Using OS X v10.10.1 (Yosemite) and Xcode 6.1.1.
I feel I must be overlooking something simple and obvious, but I just can't see it. I'm parsing the parts of an NSDate whose value is just the system date.
NSDate *currentDate = [NSDate date];
NSCalendar *systemCalendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [systemCalendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:currentSystemDate];
I can then get the seconds as
NSInteger seconds = [dateComponents second];
but this only gives me the whole number of seconds. I need the decimal portion of the seconds as well. According to the documentation. I can use
NSInteger nanos = [dateComponents nanoseconds];
to get the "number of nanosecond units for the receiver". It doesn't work however. The method is legitimate, but nanos is set to the maximum value of NSInteger. This is (I presume) because I have not supplied a suitable constant in the components method. The problem with that is that there is no NSNanosecondCalendarUnit constant and I cannot find any suitable alternative.
I could construct a new date using the date components and then subtract the two to get an NSTimeInterval which I believe will give me what I need, but I'm finding it hard to believe that there isn't a more straightforward way to get it.
Is there a simple straightforward way in Cocoa to calculate the sub-second portion of an NSDate or do I have to use the approach I just suggested?
You are using the older deprecated names for the components. The new names include NSCalendarUnitNanosecond. Take a look at NSCalendar.h, and you'll see the old and new names.
NSDateComponents *dateComponents = [systemCalendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitNanosecond) fromDate:currentSystemDate];

Is there a way to get time ago in words in rubymotion

Is there a way to get time ago in words in rubymotion such that when I have a date I can get the 2 days ago 5 days ago etc. from the current date? like its possible to do in rails.
The date format I am getting from mysql database is TIMESTAMP and the returned data is like this:
2014-05-17 22:59:07
2011-05-17 22:56:02
Question
How can I convert the above string to NSDate and then compare it to today's date and get "X days ago" etc...
You can use BubbleWrap to convert your timestamp into a NSDate instance.
The Time Ruby class was added a class level method to convert a
iso8601 formatted string into a Time instance.
Time.iso8601("2012-05-31T19:41:33Z")
=> 2012-05-31 21:41:33 +0200
Then you can do as you wish with it, sugar cube has some nice extensions for NSDate.
More answers here...
Looks like there is a CocoaPod that can handle that: https://github.com/MatthewYork/DateTools
Add pod 'DateTools' to your Rakefile. Run rake pod:install and now you can convert your date objects like this:
my_date = NSDate.dateWithNaturalLanguageString('2015-08-18T23:58:40.000+00:00')
my_date.timeAgoSinceNow

Has anyone implemented an alternative to missing Drools Temporal Unit: Years and Months

Drools seems to support only days, hours, minutes, seconds and milliseconds for the units of time used with Temporal Operators. I am working on a rule that looks for people within a particular age group. For example: between 6 months and 5 years, younger than 18 years, older than 12 years etc..
My person Class has a dateOfBirth instance variable, but no person.age method to do a direct comparison like:
$p : Person(age <= 18)
I don't have too much liberty to modify the Person Class and I am trying to avoid writing utils methods and using 'eval' for comparing, as 'evals' are not optimized. I am a drools newbie and have written the following rule.
rule "Under18Years"
when
now : RuleTime()
$p : Person(dateOfBirth after[-6570d, 0s] $now )
then
System.out.println("Under18Years fired");
end
I know this isn't very accurate as I just used 6570 (365*18) days; ignoring the leap years. I might be better off using seconds in a standard SI year (31,556,926) times 18 to account for 18 years, but is there a better way? This doesn't work for conditions involving months either. Does anyone have any other ideas/solution to this problem?
You can create a package or a function that does the above
public int getAge() {
Years years = Years.yearsBetween(dateOfBirth, currentDate);
return years.getYears();
For aslong as i have been using drools there has allways been years available to call upon. i see this was posted a while ago and maybe you have found out since.

Ruby - time.now in UTC [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I Convert DateTime.now to UTC in Ruby?
How do I get current time in Date-time-milliseconds & UTC?
Ex. 2012-03-22T18:48:40.873Z
I tried -
Time.now.utc_offset.to_s
Time.now.xs_datetime
Time.now.utc is what you must be using.

Resources