How to get the first and last date of the current year?
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS EndOfYear
The above query gives a datetime value for midnight at the beginning of December 31. This is about 24 hours short of the last moment of the year. If you want to include time that might occur on December 31, then you should compare to the first of the next year, with a <
comparison. Or you can compare to the last few milliseconds of the current year, but this still leaves a gap if you are using something other than DATETIME (such as DATETIME2):
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS LastDayOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0) AS FirstOfNextYear,
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)) AS LastTimeOfYear
Other Periods
This approach has two nice aspects: good performance and it can easily be changed for other periods by replacing both occurrences of yy
(=year) with a different string:
yy, yyyy year
qq, q quarter
mm, m month
wk, ww week
(Be careful of weeks: the starting day depends on server settings.)
Tech Details
This works by figuring out the number of years since 1900 with DATEDIFF(yy, 0, GETDATE())
and then adding that to a date of zero = Jan 1, 1900. This can be changed to work for an arbitrary date by replacing the GETDATE()
portion or an arbitrary year by replacing the DATEDIFF(...)
function with "Year - 1900."
SELECT
DATEADD(yy, DATEDIFF(yy, 0, '20150301'), 0) AS StartOfYearForMarch2015,
DATEADD(yy, 2015 - 1900, 0) AS StartOfYearFor2015
how to get first and last date of the current year javascript
This is how you can calculate the current date:
var currentDate = new Date();
You can instantiate Date
using year, month and day, but keep in mind that month is indexed from 0:
var theFirst = new Date(currentDate.getFullYear(), 0, 1);
var theLast = new Date(currentDate.getFullYear(), 11, 31);
How to get the first and last date of the month with Year and Month in python
From monthrange() you will get the first day and the number of days in that month. You can use the datetime method to convert it to date.
month =3
year= 2022
first, last = calendar.monthrange(year, month)
print(first, last)
print(datetime.datetime(year, month, 1))
print(datetime.datetime(year, month, last))
how to get first date and last date from month and year in javascript
Try the following to get your expected output:
function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);
console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));
}
GetFirstAndLastDate(2021, 10);
Get date object for the first/last day of the current year
The only real improvement that comes to mind is to give your variables more descriptive names than a
and b
.
How to get the first day and the last day of the current year in c#
This?
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31);
How to get a year first date and last date in javascript
var year = 2016 //Any Year
var first_date = new Date(year, 0, 1);
var last_date = new Date(year, 11, 31);
document.write(first_date);
document.write("<br/>");
document.write(last_date);
Moment js get first and last day of current month
In case anyone missed the comments on the original question, you can use built-in methods (works as of Moment 1.7):
const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD hh:mm');
Related Topics
The New Pivot Function in Bigquery
Concatenate Results from a SQL Query in Oracle
How to Delete from Multiple Tables in the Same SQL Statement
Insert Command :: Error: Column "Value" Does Not Exist
How to Design a Database for User Defined Fields
How to Create a Copy of an Oracle Table Without Copying the Data
Select Data from Date Range Between Two Dates
What Is a Self Join For? (In English)
Add Unique Constraint to Combination of Two Columns
Transfer Data from One Database to Another Database
How to Return Multiple Rows from the Stored Procedure? (Oracle Pl/Sql)
Select for Update with SQL Server
How to Declare Global Variable in SQL Server..
Call a Stored Procedure with Another in Oracle
Get the Distinct Sum of a Joined Table Column