I want to set the day of month in a Date
to start date of current month (01
). Now I use the following:
currentDate <- Sys.Date() #for getting current system date eg:2012-11-06 formatDate <- format(currentDate, "%Y-%m") #it return 2012-11 startDate <- as.Date(paste(formatDate, "-01", sep = "")) # 2012-11-01
Is there any easy way to do this?
Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month. To perform the previous example with the EOMONTH function, we need to use the formula =EOMONTH(A2,-1)+1 in cell B2.
How to extract month name from date in Excel. In case you want to get a month name rather than a number, you use the TEXT function again, but with a different date code: =TEXT(A2, "mmm") - returns an abbreviated month name, as Jan - Dec. =TEXT(A2,"mmmm") - returns a full month name, as January - December.
Getting First Day of the MonthSELECT m1 = DATEADD(DAY,1-DATEPART(DAY,@today),@today), m2 = CONVERT(date,CONCAT(YEAR(@today),RIGHT('0'+RTRIM(MONTH(@today)),2),'01'));
To find the first day of a month we will calculate the last day of the previous month and add one day.
Yes, there is a one-liner for that using function cut
:
cut(Sys.Date(), "month") [1] 2012-11-01 Levels: 2012-11-01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With