Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change day of the month in a Date to first day (01)

Tags:

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?

like image 857
Nandu Avatar asked Nov 06 '12 09:11

Nandu


People also ask

How do I convert a date to the first day of the month in Excel?

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 do I get the month as 01 in Excel?

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.

How do I convert date to first day of month in SQL?

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'));

How do you determine the first day of the month?

To find the first day of a month we will calculate the last day of the previous month and add one day.


1 Answers

Yes, there is a one-liner for that using function cut:

cut(Sys.Date(), "month") [1] 2012-11-01 Levels: 2012-11-01 
like image 194
plannapus Avatar answered Nov 02 '22 18:11

plannapus