How to Get the First Day of the Current Week and Month

How to get first and last day of the current week in JavaScript


var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6

var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

firstday
"Sun, 06 Mar 2011 12:25:40 GMT"
lastday
"Sat, 12 Mar 2011 12:25:40 GMT"

This works for firstday = sunday of this week and last day = saturday for this week. Extending it to run Monday to sunday is trivial.

Making it work with first and last days in different months is left as an exercise for the user

How to get the first day of the current week and month?

This week in milliseconds:

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());

// start of the next week
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("Start of the next week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());

This month in milliseconds:

// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

// get start of the month
cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("Start of the month: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());

// get start of the next month
cal.add(Calendar.MONTH, 1);
System.out.println("Start of the next month: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());

How to get first and last day of current week when days are in different months?

The getDay method returns the number of the day in the week, with Sunday as 0 and Saturday as 6. So if your week starts on Sunday, just subtract the current day number in days from the current date to get the start, and add 6 days get the end, e.g.





function getStartOfWeek(date) {



// Copy date if provided, or use current date if not

date = date? new Date(+date) : new Date();

date.setHours(0,0,0,0);



// Set date to previous Sunday

date.setDate(date.getDate() - date.getDay());



return date;

}


function getEndOfWeek(date) {

date = getStartOfWeek(date);

date.setDate(date.getDate() + 6);

return date;

}



document.write(getStartOfWeek());


document.write('<br>' + getEndOfWeek())


document.write('<br>' + getStartOfWeek(new Date(2016,2,27)))


document.write('<br>' + getEndOfWeek(new Date(2016,2,27)))

How can I get the first and last day of the week crossing months in javascript

You can use date-fns library for that:

const start = startOfWeek(date);
const end = endOfWeek(date);

Check out these threads for more solutions:

  • How to get first and last day of current week when days are in different months?
  • How to get first and last day of the week in JavaScript

How to get the 1st date and last date of the current Week as well as Month in the format of 'yyyy-mm-dd' using App Scripts

You can use toLocaleString("sv-SE") to convert the date to the desired format yyyy-mm-dd.

Try this:





const today = new Date(); 
const firstWeekD = today.getDate() - today.getDay()-1;
const lastWeekD = firstWeekD + 6;
const firstdayOfWeek = new Date(today.setDate(firstWeekD)).toLocaleString("sv-SE").slice(0,10);
const lastdayOfWeek = new Date(today.setDate(lastWeekD)).toLocaleString("sv-SE").slice(0,10);
const date = new Date();
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1).toLocaleString("sv-SE").slice(0,10);
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).toLocaleString("sv-SE").slice(0,10);

console.log(firstdayOfWeek); // first day of the week - Saturday
console.log(lastdayOfWeek); // last day of the week - Friday
console.log(firstDayOfMonth) // first day of the month
console.log(lastDayOfMonth) // last day of the month

How to get the first, second, third, and fourth week of the month?

Below method return next weekday's DateTime what you want from now or specific day.

DateTime getNextWeekDay(int weekDay, {DateTime from}) {
DateTime now = DateTime.now();

if (from != null) {
now = from;
}

int remainDays = weekDay - now.weekday + 7;

return now.add(Duration(days: remainDays));
}

The weekday parameter can be came like below DateTime const value or just int value.

class DateTime {
...
static const int monday = 1;
static const int tuesday = 2;
static const int wednesday = 3;
static const int thursday = 4;
static const int friday = 5;
static const int saturday = 6;
static const int sunday = 7;
...
}

If you want to get next Monday from now, call like below.

DateTime nextMonday = getNextWeekDay(DateTime.monday);

If you want to get next next Monday from now, call like below.

Or you just add 7 days to 'nextMonday' variable.

DateTime nextMonday = getNextWeekDay(DateTime.monday);


DateTime nextNextMonday = getNextWeekDay(DateTime.monday, from: nextMonday);
or
DateTime nextNextMonday = nextMonday.add(Duration(days: 7));

Get the first day of all weeks of current month in iOS swift

You can create a DateComponents with the components:

year: someYear,
month: someMonth,
weekday: someCalendar.firstWeekday,
weekOfMonth: n

use Calendar.date(from:) to get the Date corresponding to those components. Now you just need to vary n from 1 to 5, and put each result into an array.

var firstsOfWeek = [Date]()
let year = 2020
let month = 10
let calendar = Calendar.current

for n in 1...5 {
let dc = DateComponents(
year: year,
month: month,
weekday: calendar.firstWeekday,
weekOfMonth: n
)
firstsOfWeek.append(calendar.date(from: dc)!)
}

However, date(from:) will return a date that is not in someMonth if the first week of someMonth is only partially inside someMonth. For example, (assuming the week starts on a Sunday) the first week of October 2020 starts on 27 September, and date(from:) will give you that date if you ask it what the date corresponding to weekOfMonth: 1 is.

If you don't want that date. simply add a check to only include the start of month if it is a start of week as well, and change the for loop range to 2...5:

let firstDayOfMonth = calendar.date(from: DateComponents(year: year, month: month))!
if calendar.component(.weekday, from: firstDayOfMonth) == calendar.firstWeekday {
firstsOfWeek.append(firstDayOfMonth)
}
for n in 2...5 {
...

But do note that if you do this, the resulting array will sometimes have 4 elements, and sometimes 5 elements.



Related Topics



Leave a reply



Submit