Have you faced a situation when you need to find the first day and last day of a month? Such scenarios occurs when we do some kind of enterprise application when accounting or billing modules comes into the picture.
And YES its important as well. Lets find out in some of the technologies.
#JAVASCRIPT
Sometimes we have to send the dates from front end. Lets say by default you have to set date range components from 1st day of this month to current date. So here is the hack.
var date = new Date(), y = date.getFullYear(), m = date.getMonth(); var firstDay = new Date (y,m,1); // hard-coded 1 for first day var lastDay = new Date (y, m+1, 0); // increasing month by 1 and setting day 0
First we need to find out the components of current date as we need current month and year. And rest code is very simple to understand.
#PHP
php
requires such situation when say, we need to put some conditions of something like that. Let’s take a look at the code.
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day $last_day_this_month = date('m-t-Y');
Where
Y
gives the current yearm
is montht
is total no of days.
#MySql
Now comes to the DB. This is more often used situation if we are into finance application.
We have to run queries between dates of this month, let’s say to find total expense till now in the current month and these scenarios can be endless.
Here is the query for first day.
SELECT date(DATE_FORMAT(now(),"%Y-%m-01"))
And for last day.
SELECT LAST_DAY(NOW())
Guys let me know if above helped you somewhere I will be happy to listen.
Happy Learning.