How does momentjs calculate months ago? - momentjs

I noticed something odd and I couldn't find any documentation on how momentjs calculate months ago.
Today is Oct 2nd 2016. When I call the following momentjs functions, I get
moment("20160617", "YYYYMMDD").fromNow();
"4 months ago"
moment("20160618", "YYYYMMDD").fromNow();
"3 months ago"
Can someone please explain to me why June 17th is 4 months ago and June 18th is 3 months ago?

My best guess: it has to do with the range moment calculates for each string. Look at the key for M and it's 25 - 45 days. So "one month" can last up to about 15 more days than a usual month. Counting backwards, the 18th of a month is within that 15 day period from the 2nd of the next month. The 17th would be just out of that range. This is all assuming a month is "30 days", which it seems moment does for this kind of calculation.

Related

How to calculate the index of a day with respect to 365 days of the year?

I have a data frame of daily dates from December 2018, until March 2019, and I need to find the "Index of the day" because it is the input of another function to calculate time and sunset.
The "Index of the day" is the number of days compared to the other days of the year. Example.- January 5 (5 of 365); February 1 (32 of 365), etc.
There is a yday function in the lubridate package.
lubridate::yday(ymd('2019-09-04')) #247
You can do this as follows:
as.numeric(difftime(Sys.Date(),as.Date(paste0(format(Sys.Date(),"%Y"),'-01-01')),units = "days"))
For today the result is
246

Representing an entire day or week or month as a number like timestamp

How can a day or week or month, essentially a range of time be represented by a single number?
The next interval would represent a number 1 more than the number for the previous interval, just how the next second is 1 more than the previous second, in timestamp representation.
Given a bunch of such numbers, the larger number simply means its representing a time interval afterwards in time, when compared to a number smaller than it.
Just realized if I stick to UTC and represent the day as YYYYMMDD, this becomes a number that I am looking for.
20180420 // 20 april 2018
20180421 // 21 april 2018
20180510 // 10 may 2018
20190101 // 1 jan 2019
This works for representing a day perfectly, I think.
For week, maybe do ceil() of days of current month divided by 7 for representing week as a number W and then using the format: YYYYMMW.
2018043 // 3rd week of april 2018
2018045 // 5th week of april 2018, though may not be the 5th week semantically but representation model works, greater than 4th week of april 2018 and smaller number than 1st week of may 2018
For month, simply YYYYMM works.
I feel so smart right now! 😄

girimi839y80904040mkrkellieimimdikiifirimKINOUBIGIAIIOVIubiuuiioioaa ufu Why do we saythat there 52 weeks in a year? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Am I crazy, I've always thought there were 52 weeks in a year, a check on google returns numerous results stating the same...
But if I create a simple spreadsheet, with column A containing 1 to 365 and column B containing INT(A1 / 7) repeated 365 times, column B contains the week index corresponding to the 'julian' day in column A.
The weeks go from 0 to 52, this is actually 53 weeks. If the 1st of January is on day 0, then the 31st of December must overlay into week 1 of the next year.
Can some help explain why we say 52 weeks and not 53?
Sorry I know this isn't strictly a coding question, but is is very relative to a lot of problems with dates and coding.
There are 52 complete weeks in a year. The year has 365 days, leaving one extra day. A leap year has 366 days, adding a second extra day. This makes 52 1/7 weeks in a normal year and 52 2/7 weeks in a leap year..
An ISO week-numbering year (also called ISO year informally) has 52 or 53 full weeks, that is 364 or 371 days instead of the usual 365 or 366 days. The extra week is sometimes referred to as a leap week, although ISO 8601 does not use this term.
The ISO week date system is effectively a leap week calendar system that is part of the ISO 8601 date and time standard issued by the (ISO) since 1988 and, before that, it was defined in ISO (R) 2015 since 1971. It is used (mainly) in government and business for fiscal years, as well as in timekeeping. This was previously known as "Industrial date coding". The system specifies a week year atop the Gregorian calendar by defining a notation for ordinal weeks of the year.
Weeks start with Monday. Each week's year is the Gregorian year in which the Thursday falls. The first week of the year, hence, always contains 4 January. ISO week year numbering therefore slightly deviates from the Gregorian for some days close to 1 January.

Epidemiological curve using R and epitools gives wrong epiweeks

I have been trying to make epidemic curves in R with the epitools package. According to the documentation, you can automatically generate epidemiological weeks from a date variable, similar as I have been doing for long in STATA. Fyi, epidemiological weeks are different from yearly week numbers:
The first epi week of the year ends, by definition, on the first
Saturday of January, as long as it falls at least four days into the
month. Each epi week begins on a Sunday and ends on a Saturday.
http://www.cmmcp.org/epiweek.htm
https://wiki.ecdc.europa.eu/fem/f/1287/p/576/681.aspx#681
graphlabels<-epicurve.weeks(DATE_variable,
axisnames=FALSE,
xlab = "Week of Year",
ylab = "Cases per week",
tick.offset = 0.5,
col=mypalette)
axis(1,
at = graphlabels$xvals,
labels = graphlabels$cweek,
tick = FALSE,
line = 0,
)
This automatically generated a nice curve:
"Hurray!!", I thought. But, alas, being a scientist I double checked the data using STATA as well as manually and noticed that the data does not correspond!
Am I doing something wrong with epitool package that it gives me wrong epic weeks?
After some further discussion and research, there are differences in how epidemiological weeks can be calculated.
EPITOOLS as.week
In public health, reportable diseases are often reported by 'disease
week' (either week of reporting or week of symptom onset). In R, weeks
are numbered from 0 to 53 in the same year. The first day of week 1
starts with either the first Sunday or Monday of the year. Days before
week 1 are numbered as 0s. In contrast to R, the as.week function
generates weeks numbered from 1 to 53. The week before week 1 takes on
the value (52 or 53) from the last week of the previous year. The
as.week functions facilitates working with multiple years and
generating epidemic curves.
MMWR week
Values for MMWR week range from 1 to 53, although most years consist
of 52 weeks. The first day of any MMWR week is Sunday. MMWR week
numbering is sequential beginning with 1 and incrementing with each
week to a maximum of 52 or 53. MMWR week #1 of an MMWR year is the
first week of the year that has at least four days in the calendar
year. For example, if January 1 occurs on a Sunday, Monday, Tuesday or
Wednesday, the calendar week that includes January 1 would be MMWR
week #1. If January 1 occurs on a Thursday, Friday, or Saturday, the
calendar week that includes January 1 would be the last MMWR week of
the previous year (#52 or #53). Because of this rule, December 29, 30,
and 31 could potentially fall into MMWR week #1 of the following MMWR
year.
EPIWEEK for STATA
Each epidemiological week begins on a Sunday and ends on Saturday. And
the first epidemiological week of year ends on the first Saturday of
January, provided that it falls at least four or more days into the
month
ISO (or business) week
An ISO week-numbering year (also called ISO year informally) has 52 or
53 full weeks. That is 364 or 371 days instead of the usual 365 or 366
days. The extra week is referred to here as a leap week, although ISO
8601 does not use this term. Weeks start with Monday. The first week
of a year is the week that contains the first Thursday of the year
Week "u" in SAS
Each epidemiological week begins on a Sunday and ends on Saturday. And
the first epidemiological week of year contains Sunday. Weeks range
from 0 to 53 but do not always count 7 days

Get data of exactly one year out of an xts time series

I'm programming a function in which I want to plot let's say the stock price for any stock during the last 365 days. However not trading days, but just a one year period. So if I'm generating the chart on the 15th of February I want data from the 16th February 2012-15th February 2013. How can I do that? last() for example shows me the last 365 trading days or days of which there is data available.
library(quantmod)
getSymbols("AAPL")
plot(AAPL)
User character subsetting.
AAPL[paste(Sys.Date()-365, Sys.Date(), sep="/")]

Resources