Change Day of the Month in a Date to First Day (01)

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

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

cut(Sys.Date(), "month")
[1] 2012-11-01
Levels: 2012-11-01

How to change the day in datetime, to the first day of the current month


  • Use pandas.offsets.MonthBegin to set the day to the beginning of the month.
  • Also Pandas: convert date in month to the 1st day of next month
  • pandas User Guide: Time series / date functionality
  • pandas: Date offsets
    • MonthBegin
  • method 1 sets the day to the beginning of the month, but if the day is already the 1st, then the date is set to the first of the previous month.
  • method 2 first sets the date to the end of the month, then to the first of the month.
import pandas as pd

# sample data
df = pd.DataFrame({'date': [pd.Timestamp('1994-01-31 00:00:00'), pd.Timestamp('1994-02-28 00:00:00'), pd.Timestamp('1994-03-30 00:00:00'), pd.Timestamp('1994-04-29 00:00:00'), pd.Timestamp('1994-05-31 00:00:00'), pd.Timestamp('2010-01-01 00:00:00'), pd.Timestamp('2010-01-31 00:00:00'), pd.Timestamp('2010-03-02 00:00:00'), pd.Timestamp('2010-04-01 00:00:00'), pd.Timestamp('2010-05-01 00:00:00'), pd.Timestamp('2010-05-31 00:00:00'), pd.Timestamp('2020-06-30 00:00:00'), pd.Timestamp('2020-07-31 00:00:00'), pd.Timestamp('2020-08-31 00:00:00'), pd.Timestamp('2020-09-30 00:00:00'), pd.Timestamp('2020-10-09 00:00:00')]})

# method 1
df['m1'] = df.date - pd.offsets.MonthBegin(1)

# method 2
df['m2'] = df.date - pd.offsets.MonthEnd(0) - pd.offsets.MonthBegin(1)

Sample Image

  • As noted by ALollz in a comment, there can be issues with method 1, if the day of the month is already the first.
    • At his suggestion, is a more robust solution, method 2, which accounts for days on the 1st.

How can I get the first day of the next month in Python?

Here is a 1-line solution using nothing more than the standard datetime library:

(dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)

Examples:

>>> dt = datetime.datetime(2016, 2, 29)
>>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1))
2016-03-01 00:00:00

>>> dt = datetime.datetime(2019, 12, 31)
>>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1))
2020-01-01 00:00:00

>>> dt = datetime.datetime(2019, 12, 1)
>>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1))
2020-01-01 00:00:00

MySQL - Change date to the first day of the month

To the first day, d1 is 01 everytime.

UPDATE [table]
SET dateField = CONCAT(YEAR(dateField),'-',MONTH(dateField),'-01')
WHERE [conditions]

finding first day of the month in python

This is a pithy solution.

import datetime 

todayDate = datetime.date.today()
if todayDate.day > 25:
todayDate += datetime.timedelta(7)
print todayDate.replace(day=1)

One thing to note with the original code example is that using timedelta(30) will cause trouble if you are testing the last day of January. That is why I am using a 7-day delta.

How to get date representing the first day of a month?

The best and easiest way to do this is to use:

SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)

Just replace GETDATE() with whatever date you need.

Getting the first and last day of a month, using a given DateTime object

DateTime structure stores only one value, not range of values. MinValue and MaxValue are static fields, which hold range of possible values for instances of DateTime structure. These fields are static and do not relate to particular instance of DateTime. They relate to DateTime type itself.

Suggested reading: static (C# Reference)

UPDATE: Getting month range:

DateTime date = ...
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

How to convert date to the first day of month in a PySpark Dataframe column?

You can use trunc:

import pyspark.sql.functions as f

df.withColumn("first_date", f.trunc("date", "month")).show()

+----------+----------+
| date|first_date|
+----------+----------+
|2017-11-25|2017-11-01|
|2017-12-21|2017-12-01|
|2017-09-12|2017-09-01|
+----------+----------+

Simplest way to create a date that is the first day of the month, given another date


Select DateAdd(Month, DateDiff(Month, 0, GetDate()), 0)

To run this on a column, replace GetDate() with your column name.

The trick to this code is with DateDiff. DateDiff returns an integer. The second parameter (the 0) represents the 0 date in SQL Server, which is Jan 1, 1900. So, the datediff calculates the integer number of months since Jan 1, 1900, then adds that number of months to Jan 1, 1900. The net effect is removing the day (and time) portion of a datetime value.



Related Topics



Leave a reply



Submit