Find the date of the end of the month in various languages (C #, Java, Ruby, Javascript, VBA, Excel, MySQL, SQLite3, Oracle)

Introduction

When developing systems and tools for business use, the last day of the month may sometimes be required, for example, "the billing date in the invoice" or "the execution date of the closing process". Therefore, I investigated a typical method for finding the date of the end of the month in various languages.

C#

LastDayOfMonth.cs


public class LastDayOfMonth
{
    public static void CalcLastDayOfMonth()
    {
        DateTime today = DateTime.Now;
        int days = DateTime.DaysInMonth(today.Year, today.Month);
        System.Console.WriteLine(new DateTime(today.Year, today.Month, days).ToString("yyyy-MM-dd"));
    }
}

Java

LastDayOfMonth.java


import java.time.LocalDate;
import java.time.YearMonth;

public class LastDayOfMonth {
	public static void main(String[] args) {
		LocalDate lastDay = YearMonth.now().atEndOfMonth();
		System.out.println(lastDay.toString());
	}
}

Ruby

irb


irb(main):001:0> require 'date'
=> true
irb(main):002:0> today = Date.today
=> #<Date: 4917313/2,0,2299161>
irb(main):003:0> puts Date.new(today.year, today.month, -1)
2019-06-30
=> nil

Javascript

var today = new Date();
var last_day = new Date(today.getFullYear(), today.getMonth() + 1, 0);
document.write(last_day);

VBA

Module1


Public Sub PrintLastDayOfMonth()
  Debug.print DateSerial(Year(Now()), Month(Now())+1, 0)
End Sub

Excel (worksheet function)

=DATE(YEAR(NOW()),MONTH(NOW())+1,0)

=EOMONTH(NOW(),0)

MySQL

mysql> SELECT LAST_DAY(NOW());
+-----------------+
| LAST_DAY(NOW()) |
+-----------------+
| 2019-06-30      |
+-----------------+
1 row in set (0.00 sec)

SQLite3

sqlite> select date('now', '+1 months', 'start of month', '-1 days');
2019-06-30

Oracle

SELECT LAST_DAY(SYSDATE) FROM DUAL;

LAST_DAY(SYSDATE)
-----------------
29-FEB-20

Summary

language Class to use/Method/function How to find the last day
C# DateTime "Any year and month" and "in that year and month""Days of the month"".
Java(Java8~) YearMonth Specify "arbitrary date".
Ruby Date "Any year and month-Specify "1 day".
Javascript Date Specify "0th day of the month following any year and month".
VBA DateSerial Specify "0th day of the month following any year and month".
Excel (worksheet function) DATE Specify "0th day of the month following any year and month".
Excel (worksheet function) EOMONTH Specify "0th day of any year and month".
MySQL LAST_DAY Specify "arbitrary date".
SQLite3 date Specify "one day before the first day of the month following any date".
Oracle LAST_DAY Specify "arbitrary date".

Reference URL

Recommended Posts

Find the date of the end of the month in various languages (C #, Java, Ruby, Javascript, VBA, Excel, MySQL, SQLite3, Oracle)
Sample code to get the values of major SQL types in Java + Oracle Database 12c
How to derive the last day of the month in Java
Find the number of days in a month with Kotlin
Java end of month plusMonths
Find out the list of fonts available in AWS Lambda + Java