What's The Most Efficient Way Get The First Day of The Current Month

What's the most efficient way get the first day of the current month?

Time.parse("2009-10-26").strftime("%Y-%m-01")

How can I select the first day of a month in SQL?

SELECT DATEADD(month, DATEDIFF(month, 0, @mydate), 0) AS StartOfMonth

Get first date of current month in java

try

    Calendar c = Calendar.getInstance();   // this takes current date
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(c.getTime()); // this returns java.util.Date

Updated (Since Java 8):

import java.time.LocalDate;
LocalDate todaydate = LocalDate.now();
System.out.println("Months first date in yyyy-mm-dd: " +todaydate.withDayOfMonth(1));

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

UPDATE: From comments (@KarlGjertsen & @SergeyBerezovskiy)

DateTime date = ...
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddSeconds(-1);
//OR
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddTicks(-1);

How can I find the first and last date in a month using PHP?

The easiest way is to use date, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.

// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
$last_day_this_month = date('m-t-Y');

// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));

date() searches the string it's given, like 'm-t-Y', for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:

  • Y gives you the 4-digit year from the timestamp ('2010')
  • m gives you the numeric month from the timestamp, with a leading zero ('04')
  • t gives you the number of days in the timestamp's month ('30')

You can be creative with this. For example, to get the first and last second of a month:

$timestamp    = strtotime('February 2012');
$first_second = date('m-01-Y 00:00:00', $timestamp);
$last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!

See http://php.net/manual/en/function.date.php for other symbols and more details.

What is the simplest and most efficient way to get the first and last date of the previous month?

For dates I strongly recommend not using BETWEEN. This is highlighted by your need to remove 3ms from a date to get "the last moment of the previous day".

With continuous values (rather than discrete values), that can have varying degrees of accuracy, it is generally better to use >= AND <. For example...

WHERE myDateField >= '2012-04-01' AND myDateField < '2012-05-01'

By doing this you never need to even think about the accuracy of the myDateField data or data-type. It just works. Always.


With that in mind, your code is very close to what I would use...

SET @start = DATEADD(month, DATEDIFF(month, 0, getDate()) - 1, 0)
SET @end = DATEADD(month, DATEDIFF(month, 0, getDate()) , 0)

Netezza What is the best way to get the first day of the month for a date?

Use date_trunc('month', current_ date), which is documented here.

The first day of the current month in php using date_modify as DateTime object

Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:

<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');

// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');

// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');

In PHP 5.4+ you can do this:

<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');

echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');

If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():

<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you


Related Topics



Leave a reply



Submit