How to get all days in current month?
Here's a solution with datetime
and calendar
:
>>> import datetime, calendar
>>> year = 2014
>>> month = 1
>>> num_days = calendar.monthrange(year, month)[1]
>>> days = [datetime.date(year, month, day) for day in range(1, num_days+1)]
>>> days
[datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3), datetime.date(2014, 1, 4), datetime.date(2014, 1, 5), datetime.date(2014, 1, 6), datetime.date(2014, 1, 7), datetime.date(2014, 1, 8), datetime.date(2014, 1, 9), datetime.date(2014, 1, 10), datetime.date(2014, 1, 11), datetime.date(2014, 1, 12), datetime.date(2014, 1, 13), datetime.date(2014, 1, 14), datetime.date(2014, 1, 15), datetime.date(2014, 1, 16), datetime.date(2014, 1, 17), datetime.date(2014, 1, 18), datetime.date(2014, 1, 19), datetime.date(2014, 1, 20), datetime.date(2014, 1, 21), datetime.date(2014, 1, 22), datetime.date(2014, 1, 23), datetime.date(2014, 1, 24), datetime.date(2014, 1, 25), datetime.date(2014, 1, 26), datetime.date(2014, 1, 27), datetime.date(2014, 1, 28), datetime.date(2014, 1, 29), datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]
Get all dates of current month using Python
You can use datetime
:
from datetime import date, timedelta
d1 = date(2019, 6, 1)
d2 = date(2019, 6, 30)
delta = d2 - d1
for i in range(delta.days + 1):
print(d1 + timedelta(days=i))
Refining the code and making it independent of user specification:
from datetime import date, timedelta, datetime
import calendar
def all_dates_current_month():
month = datetime.now().month
year = datetime.now().year
number_of_days = calendar.monthrange(year, month)[1]
first_date = date(year, month, 1)
last_date = date(year, month, number_of_days)
delta = last_date - first_date
return [(first_date + timedelta(days=i)).strftime('%Y-%m-%d') for i in range(delta.days + 1)]
all_dates_current_month()
and you get:
['2019-06-01',
'2019-06-02',
'2019-06-03',
'2019-06-04',
'2019-06-05',
'2019-06-06',
'2019-06-07',
'2019-06-08',
'2019-06-09',
'2019-06-10',
'2019-06-11',
'2019-06-12',
'2019-06-13',
'2019-06-14',
'2019-06-15',
'2019-06-16',
'2019-06-17',
'2019-06-18',
'2019-06-19',
'2019-06-20',
'2019-06-21',
'2019-06-22',
'2019-06-23',
'2019-06-24',
'2019-06-25',
'2019-06-26',
'2019-06-27',
'2019-06-28',
'2019-06-29',
'2019-06-30']
get number of days in the CURRENT month using javascript
Does this do what you want?
function daysInThisMonth() {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
}
Get all days and date for a given month
try this
$list=array();
$month = 12;
$year = 2014;
for($d=1; $d<=31; $d++)
{
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month)
$list[]=date('Y-m-d-D', $time);
}
echo "<pre>";
print_r($list);
echo "</pre>";
How to get all dates and weekdays in current month in flutter
The lastDayOfMonth
is a single date, therefore using lastDayOfMonth.weekDay
gives a single day based on lastDayOfMonth
. We can add duration on lastDayOfMonth
and find day name.
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
This will provide update date based on index
. I am using intl
package or create a map to get the day name.
A useful answer about date format.
Row(
children: List.generate(
lastDayOfMonth.day,
(index) => Padding(
padding: const EdgeInsets.only(right: 24.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"${index + 1}",
),
() {
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
final dateName =
DateFormat('E').format(currentDate);
return Text(dateName);
}()
],
),
),
),
),
Find all the days in a month with Date object?
To get a list of all days in a month, you can start with a Date
on the first day of a month, increase the day until the month changes.
/**
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @return {Date[]} List with date objects for each day of the month
*/
function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}
UTC Version
In response to some comments, I've created a version that uses UTC methods in case you want to call UTC methods instead of the standard methods that return the localized time zone.
I suspect this is the culprit of the comments saying this didn't work. You typically want to make sure you call getUTCMonth/Day/Hours
methods if you instantiated it with Date.UTC
, and vice-versa, unless you are trying to convert time zones and show differences.
function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}
Editing This Answer
If you think there's a problem with this script, please feel free to:
- First see existing unit tests below
- Write a test case that proves it's broken.
- Fix the code, making sure existing tests pass.
Unit Tests
/**
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @return {Date[]} List with date objects for each day of the month
*/
function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}
function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}
const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
describe("getDaysInMonthUTC", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonthUTC(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonthUTC(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
describe("getDaysInMonth", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonth(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonth(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet"/>
how to get all weeks of current month
You have to use this code, I have to modify and add some lines. It will help you
public static void getWeeksOfMonth(int month, int year) {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(DAY_OF_MONTH);
System.out.println(ndays + "<<<ff");
while (cal.get(DAY_OF_WEEK) != FRIDAY) {
cal.add(DAY_OF_MONTH, 1);
ndays--;
}
int remainingDays = ndays%7;
if(remainingDays==0)
ndays += 7;
else
ndays = ndays + 7 - remainingDays;
int inc = 1;
for (int i = 1; i <= ndays; i++) {
String day = sdf.format(cal.getTime());
System.out.println(day + "<<<");
inc++;
if (i % 7 == 0) {
Log.e("question", "=======week days===========");
inc = 0;
}
if (inc >= 1 && i == ndays) {
for (int ii = inc; ii <= 6; ii++) {
String dayi = sdf.format(cal.getTime());
System.out.println(dayi + "<<<");
Log.e("quest1", dayi + "<<<");
inc++;
}
}
cal.add(Calendar.DATE, 1);
}
}
My Output Is
Friday 03-Feb-2017<<<
Saturday 04-Feb-2017<<<
Sunday 05-Feb-2017<<<
Monday 06-Feb-2017<<<
Tuesday 07-Feb-2017<<<
Wednesday 08-Feb-2017<<<
Thursday 09-Feb-2017<<<
=====week days===========
Friday 10-Feb-2017<<<
Saturday 11-Feb-2017<<<
Sunday 12-Feb-2017<<<
Monday 13-Feb-2017<<<
Tuesday 14-Feb-2017<<<
Wednesday 15-Feb-2017<<<
Thursday 16-Feb-2017<<<
=====week days===========
Friday 17-Feb-2017<<<
Saturday 18-Feb-2017<<<
Sunday 19-Feb-2017<<<
Monday 20-Feb-2017<<<
Tuesday 21-Feb-2017<<<
Wednesday 22-Feb-2017<<<
Thursday 23-Feb-2017<<<
=====week days===========
Friday 24-Feb-2017<<<
Saturday 25-Feb-2017<<<
Sunday 26-Feb-2017<<<
Monday 27-Feb-2017<<<
Tuesday 28-Feb-2017<<<
Wednesday 01-Mar-2017<<<
Thursday 02-Mar-2017<<<
=====week days===========
How to get the number of days in a month
Days left in current month
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentMonthLastDate = (new Date(currentYear, currentMonth, 0)).getDate();
var daysLeftInMonth = currentMonthLastDate - currentDate.getDate();
console.log(daysLeftInMonth);
Get Array of Dates for Current Month in jQuery?
You can do this like below:
Edited
var date = new Date();
var month = date.getMonth();
date.setDate(1);
var all_days = [];
while (date.getMonth() == month) {
var d = date.getFullYear() + '-' + date.getMonth().toString().padStart(2, '0') + '-' + date.getDate().toString().padStart(2, '0');
all_days.push(d);
date.setDate(date.getDate() + 1);
}
console.log(all_days);
Related Topics
How to Find Words in a List That Starts With a Certain Letter the User Asked For
How to Use a Module Without Installing It on Your Computer
Convert CSV File to Pipe Delimited File in Python
Pandas: Merging Two Columns into One With Corresponding Values
How to Loop Over Grouped Pandas Dataframe
Recursive Function to Go Inside Dictionary of Dictionary
Large File Crashing on Jupyter Notebook
How to Convert SQL Query Results into a Python Dictionary
Navigating Through Pagination With Selenium in Python
Check If List Is Ascending or Descending (Using For)
How to Force Pip to Reinstall the Current Version
Get Absolute Paths of All Files in a Directory
Arrange a Text File Using Python
How to Append New Data Onto a New Line
Pandas: How to Return Rows Where a Column Has a Line Breaks/New Line ( \N ) in Its Cell
Running Multiple Commands Simultaneously from Python
Pass Variable Between Python Scripts
How to Obtain Second and Fourth Word from Each Line in a File