In v1 I could render change the columnFormat to something like
'dddd MMMM d'
which would output for example:
Sunday January 25, Monday January 26, Tuesday January 27
I wanted to upgrade to the v2 beta and now the same format renders this:
Sunday January 0, Monday January 1, Tuesday January 2
This occurs on any month or week view. It always renders d starting at 0. The docs haven't changed. d is a valid moment.js formatting character so I'm really not sure what's going on.
In moment.js d is the day of the week. D is the day of the month.
Use D instead of d.
Related
In R, I have a dataset with the week number and the year (see below) and I want to transform it into the date the corresponding Monday of the week.I used the as.Date() function. This works well, except for the first week, where the function return NA as the corresponding Monday of the first week belongs to the previous year. I thus want that the function returns the date of Monday, even if it is not the same year. Any idea?
data.frame(week = paste(2022,0:5,sep="-")) %>%
mutate(week2 = paste(week,"1",sep="-"),
date=as.Date(week2, "%Y-%W-%w"))
week week2 date
1 2022-0 2022-0-1 <NA>
2 2022-1 2022-1-1 2022-01-03
3 2022-2 2022-2-1 2022-01-10
4 2022-3 2022-3-1 2022-01-17
5 2022-4 2022-4-1 2022-01-24
6 2022-5 2022-5-1 2022-01-31
Your code is good. But you have only one problem: NA if week == 0. If week number is zero then first day of the year is not monday. So you can check if week number is zero (and find monday in previous year) or do as in you example. For example as.Date("2018-1-1", "%Y-%W-%w") == 2018-01-01 (week == 1, not 0).
foo <- function(year, week) {
if (week == 0) {
year <- year - 1
week <- data.table::week(as.Date(paste0(year, "-12-31"))) - 1
}
return(as.Date(paste0(c(year, week, "1"), collapse = "-"), "%Y-%W-%w"))
}
I would like to mutate a fiscal month-end date to a dataset in R. In my company the fiscal month-end would be on 21st of that. For example
12/22/2019 to 1/21/2020 will be Jan-2020
1/22/2020 to 2/21/2020 will be Feb-2020
2/22/2020 to 3/21/2020 will be Mar-2020
etc
Dataset
Desired_output
How would I accomplish this in R. The Date column in my data is %m/%d/%Y(1/22/2020)
You could extract the date and if date is greater than 22 add 10 days to it and get the date in month-year format :
transform(dat, Fiscal_Month = format(Date +
ifelse(as.integer(format(Date, '%d')) >= 22, 10, 0), '%b %Y'))
# Date Fiscal_Month
#1 2020-01-20 Jan 2020
#2 2020-01-21 Jan 2020
#3 2020-01-22 Feb 2020
#4 2020-01-23 Feb 2020
#5 2020-01-24 Feb 2020
This can also be done without ifelse like this :
transform(dat, Fiscal_Month = format(Date + c(0, 10)
[(as.integer(format(Date, '%d')) >= 22) + 1], '%b %Y'))
data
Used this sample data :
dat <- data.frame(Date = seq(as.Date('2020-01-20'), by = '1 day',length.out = 5))
1) yearmon We perform the following steps:
create test data d which shows both a date in the start of period month (i.e. 22nd or later) and a date in the end of period month (i.e. 21st or earlier)
convert the input d to Date class giving dd
subtract 21 days thereby shifting it to the month that starts the fiscal period
convert that to ym of yearmon class (which represents a year and a month without a day directly and internally represents it as the year plus 0 for Jan, 1/12 for Feb, ..., 11/12 for Dec) and then add 1/12 to get to the month at the end of fiscal period.
format it as shown. (We could omit this step, i.e. the last line of code, if the default format, e.g. Jan 2020, that yearmon uses is ok.
The whole thing could easily be written in a single line of code but we have broken it up for clarity.
library(zoo)
d <- c("1/22/2020", "1/21/2020") # test data
dd <- as.Date(d, "%m/%d/%Y")
ym <- as.yearmon(dd - 21) + 1/12
format(ym, "%b-%y")
## [1] "Feb-20" "Jan-20"
2) Base R This could be done using only in base R as follows. We make use of dd from above. cut computes the first of the month that dd-21 lies in (but not as a Date class object) and then as.Date converts it to one. Adding 31 shifts it to the end of period month and formatting this we get the final answer.
format(as.Date(cut(dd - 21, "month")) + 31, "%b-%y")
## [1] "Feb-20" "Jan-20"
I am confused about time difference between LON and APAC regions. With UK day light saving, time difference between London and Singapore is 7 hours and without daylight saving, it is 8 hours.
But time difference in Sydney is 9 hours with day light saving and 11 hours without day light saving.
So my question is why there is extra one hour difference between LON and SYD
I have tested this with below code with system time zone as London
#Test
public void datetimetest(){
LocalDateTime dt1 = LocalDateTime.of(2019, 3, 19, 11, 0);
LocalDateTime dt2 = LocalDateTime.of(2019, 4, 19, 11, 0);
ZonedDateTime zonedDateTimeLon1 = dt1
.atZone(systemDefault())
.withZoneSameInstant(ZoneId.of("Europe/London"));
ZonedDateTime zonedDateTimeLon2 = dt2
.atZone(systemDefault())
.withZoneSameInstant(ZoneId.of("Europe/London"));
printZonedDateTime(zonedDateTimeLon1, "Europe/London");
printZonedDateTime(zonedDateTimeLon1, "Australia/Sydney");
printZonedDateTime(zonedDateTimeLon1, "Asia/Singapore");
printZonedDateTime(zonedDateTimeLon2, "Europe/London");
printZonedDateTime(zonedDateTimeLon2, "Australia/Sydney");
printZonedDateTime(zonedDateTimeLon2, "Asia/Singapore");
}
private static void printZonedDateTime(ZonedDateTime zonedDateTimeLon, String timeZone) {
LocalDateTime dateTime = zonedDateTimeLon
.withZoneSameInstant(ZoneId.of(timeZone))
.toLocalDateTime();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(timeZone + "= " + dateTimeFormatter.format(dateTime));
}
Result:
Europe/London= 2019-03-19 11:00:00
Australia/Sydney= 2019-03-19 22:00:00
Asia/Singapore= 2019-03-19 19:00:00
Europe/London= 2019-04-19 11:00:00
Australia/Sydney= 2019-04-19 20:00:00
Asia/Singapore= 2019-04-19 18:00:00
As shown in results, time difference in April is 9 hours for Sydney. Can anyone please explain this
Today (March 19) Sydney is still using summer time (DST). The summer on the southern hemisphere coincides with the winter on the northern. So it’s ending around this time. So while standard time in Sydney is at offset +10:00, they are at +11:00 now. London is currently on standard time, +00:00. So the difference is 11 hours as you have observed.
Summer time begins in London (and the EU) on March 31, bringing London on offset +01:00, in turn reducing the difference to 10 hours.
Summer time ends in Sydney on April 7. They return to their standard offset of +10:00, further reducing the difference between London and Sydney to 9 hours, the difference that you observed for April 19.
Or in code:
ZoneId london = ZoneId.of("Europe/London");
ZoneId sydney = ZoneId.of("Australia/Sydney");
Instant instMarch = Instant.parse("2019-03-19T00:00:00Z");
Instant instApril = Instant.parse("2019-04-19T00:00:00Z");
System.out.println(instMarch.atZone(london));
System.out.println(instMarch.atZone(sydney));
System.out.println(instApril.atZone(london));
System.out.println(instApril.atZone(sydney));
The output is:
2019-03-19T00:00Z[Europe/London]
2019-03-19T11:00+11:00[Australia/Sydney]
2019-04-19T01:00+01:00[Europe/London]
2019-04-19T10:00+10:00[Australia/Sydney]
Please note that the current offsets are printed (Z in the first output line means offset zero).
Singapore in turn does not use summer time (at least not in 2019), so here the difference is only reduced from 8 to 7 hours when summer time begins in London.
I have a dataframe that look like the one below.
bus_date <- as.Date(c('2017-04-03', '2017-04-04', '2017-04-06', '2017-04-11', '2017-04-13', '2017-04-17'))
sales <- c(100, 110, 120, 200, 300, 100)
daily_sales <- data.frame(bus_date, sales)
It is a sales table at the daily level.
I want to create a new variable called "Week_Start" which is the date of the business week. I have implemented various solutions which allow me to record a week number (1-52) but I need the actual week starting date.
if (bus_date is a Monday)
return(bus_date)
else
return(Monday before bus_date)
So my resulting dataframe would look like:
Week_Start <- as.Date(c('2017-04-03', '2017-04-03', '2017-04-03', '2017-04-10', '2017-04-10', '2017-04-17'))
daily_sales2 <- data.frame(bus_date, sales, Week_Start)
I know there is probably an easy way to do this, but unsure where to begin. Thanks.
From ?strptime
%w Weekday as decimal number (0–6, Sunday is 0).
%W Week of the year as decimal number (00–53) using Monday as the
first day of week (and typically with the first Monday of the year as
day 1 of week 1). The UK convention.
as.Date(format(daily_sales$bus_date, "%Y-%W-1"), format = "%Y-%W-%w")
#[1] "2017-04-03" "2017-04-03" "2017-04-03" "2017-04-10" "2017-04-10" "2017-04-17"
Here's how you can do that with floor_date from lubridate. By default, floor_date gives you the preceding Sunday. +1 gives you Monday.
library(lubridate)
daily_sales$Week_Start <- floor_date(daily_sales$bus_date,unit="week")+1
daily_sales
bus_date sales Week_Start
1 2017-04-03 100 2017-04-03
2 2017-04-04 110 2017-04-03
3 2017-04-06 120 2017-04-03
4 2017-04-11 200 2017-04-10
5 2017-04-13 300 2017-04-10
6 2017-04-17 100 2017-04-17
I have rather large meteorological data sets that look something like:
year month day hour min sec temp RH Rad
I need to convert the day to the sequential day of year, e.g:
jan 1 is day 0
feb 1 is day 31
march 1 is day 59 (in non leap years)
April 1 is 90, etc.
Data are stored in a data frame, say met_dat, with met_dat$year, met_dat$day, etc.
I'd like to assign the yd based on the month, i.e.,
if met_dat$month==0, /*this is the code for january*/
then
met_dat$yd<-met_dat$day,
else if met_dat$month==1, /*this is the code for february*/
then
met_dat$yd<-met_dat$day+30
else if met_dat$month==2,
then
met_dat$yd<-met_dat$day+58
etc, for the remaining months.
I've tried nesting ifelse statements as:
met_dat$yd<-ifelse( (met_dat$month==0),met_dat$yd<-met_dat$day,
(ifelse( (met_dat$month==1), met_dat$yd<-met_dat$day+30,
(ifelse( (met_dat$month==2), met_dat$yd<-met_dat$day+58, NA) )))
My real code has all 12 months, but 12 or three, this doesn't work...it assigns incorrect values for met_dat$yd, sometimes near correct, but never correct for all months.
Any suggestions?
You can convert your data to Date using as.Date, thus turning it into an integer representation. Then simply subtract an epoch (reference) date from each value. Like this:
x <- data.frame(
year = 2012,
month = c("Jan", "Jan", "Feb", "Mar", "Apr", "Apr"),
day = c(1, 2, 1, 1, 1, 2)
)
xx <- with(x, as.Date(paste(year, month, day, sep="-"), format="%Y-%b-%d"))
xx
[1] "2012-01-01" "2012-01-02" "2012-02-01" "2012-03-01" "2012-04-01" "2012-04-02"
xx - as.Date("2012-01-01")
Time differences in days
[1] 0 1 31 60 91 92