How to get current week start date ( Monday ) and End Date ( Sunday) in R
You can use lubridate
's floor_date
and ceiling_date
with unit
as "week". By default week starts on a Sunday in lubridate
so to get start date as Monday we need to add 1 in floor_date
.
library(lubridate)
todays_date <- as.Date('2020-07-18')
floor_date(todays_date, 'week') + 1
#[1] "2020-07-13"
ceiling_date(todays_date, 'week')
#[1] "2020-07-19"
Get current week start/end date with DST
I am aware that there is more than one way to skin a cat, but in this case I was interested in how to fix my current code and more importantly to find out what's wrong with it.
Thank you all for your suggestions, especially to @misorude for pointing out the obvious flaw in my initial code, whereas "not every day has 86400 seconds", which is especially true during DST.
So here's the updated working code using relative "days" instead of fixed seconds amount:
$monday = strtotime('next Monday -1 week');
$monday = date('w', $monday)==date('w') ? strtotime(date("Y-m-d",$monday)." +7 days") : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
echo "This week start/end date:<br>";
echo $this_week_sd = date("Y-m-d",$monday)."<br>";
echo $this_week_ed = date("Y-m-d",$sunday)."<br>";
//output:
This week start/end date:
2018-10-29
2018-11-04
Once again, thank you all for your inputs. Much appreciated!
How to get start of or end of week in dart
You can get the weekday from the DateTime using https://api.dart.dev/stable/2.5.1/dart-core/DateTime/weekday.html and add/subtract this number from you date:
void main() {
final date = DateTime.parse('2019-10-08 15:43:03.887');
print('Date: $date');
print('Start of week: ${getDate(date.subtract(Duration(days: date.weekday - 1)))}');
print('End of week: ${getDate(date.add(Duration(days: DateTime.daysPerWeek - date.weekday)))}');
}
DateTime getDate(DateTime d) => DateTime(d.year, d.month, d.day);
UPDATE
Please read and upvote the answer from lrn. He knows a lot more about this stuff than me. :)
Get current week start and end date in Java - (MONDAY TO SUNDAY)
Updated answer using Java 8
Using Java 8 and keeping the same principle as before (the first day of the week depends on your Locale
), you should consider using the following:
Obtain the first and last DayOfWeek
for a specific Locale
final DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
final DayOfWeek lastDayOfWeek = DayOfWeek.of(((firstDayOfWeek.getValue() + 5) % DayOfWeek.values().length) + 1);
Query for this week's first and last day
LocalDate.now(/* tz */).with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); // first day
LocalDate.now(/* tz */).with(TemporalAdjusters.nextOrSame(lastDayOfWeek)); // last day
Demonstration
Consider the following class
:
private static class ThisLocalizedWeek {
// Try and always specify the time zone you're working with
private final static ZoneId TZ = ZoneId.of("Pacific/Auckland");
private final Locale locale;
private final DayOfWeek firstDayOfWeek;
private final DayOfWeek lastDayOfWeek;
public ThisLocalizedWeek(final Locale locale) {
this.locale = locale;
this.firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
this.lastDayOfWeek = DayOfWeek.of(((this.firstDayOfWeek.getValue() + 5) % DayOfWeek.values().length) + 1);
}
public LocalDate getFirstDay() {
return LocalDate.now(TZ).with(TemporalAdjusters.previousOrSame(this.firstDayOfWeek));
}
public LocalDate getLastDay() {
return LocalDate.now(TZ).with(TemporalAdjusters.nextOrSame(this.lastDayOfWeek));
}
@Override
public String toString() {
return String.format( "The %s week starts on %s and ends on %s",
this.locale.getDisplayName(),
this.firstDayOfWeek,
this.lastDayOfWeek);
}
}
We can demonstrate its usage as follows:
final ThisLocalizedWeek usWeek = new ThisLocalizedWeek(Locale.US);
System.out.println(usWeek);
// The English (United States) week starts on SUNDAY and ends on SATURDAY
System.out.println(usWeek.getFirstDay()); // 2018-01-14
System.out.println(usWeek.getLastDay()); // 2018-01-20
final ThisLocalizedWeek frenchWeek = new ThisLocalizedWeek(Locale.FRANCE);
System.out.println(frenchWeek);
// The French (France) week starts on MONDAY and ends on SUNDAY
System.out.println(frenchWeek.getFirstDay()); // 2018-01-15
System.out.println(frenchWeek.getLastDay()); // 2018-01-21
Original Java 7 answer (outdated)
Simply use:
c.setFirstDayOfWeek(Calendar.MONDAY);
Explanation:
Right now, your first day of week is set on Calendar.SUNDAY
. This is a setting that depends on your Locale
.
Thus, a better alternative would be to initialise your Calendar
specifying the Locale
you're interested in.
For example:
Calendar c = GregorianCalendar.getInstance(Locale.US);
... would give you your current output, while:
Calendar c = GregorianCalendar.getInstance(Locale.FRANCE);
... would give you your expected output.
How do I get the start date and end date of the current week?
Dim dayOfWeek = Cint(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(-1 * dayOfWeek)
Dim endOfWeek = DateTime.Today.AddDays(7 - dayOfWeek).AddSeconds(-1)
Console.WriteLine(startOfWeek)
Console.WriteLine(endOfWeek)
Using today's date (2016-06-19) results in
2016-06-19 12:00:00 AM
2016-06-25 11:59:59 PM
Related Topics
How to Change Options Based on Another Select Option in a Table
Ask for Geolocation Permission Again If It Was Denied
Prevent CSS Tooltip from Going Out of Page/Window
Converting Json Object to CSV Format in JavaScript
Webaudio - Seamlessly Playing Sequence of Audio Chunks
Verify External Script Is Loaded
Javascript Generate Unique Number Based on String
Replacing Multiple Elements of an Array While Keeping Their Position
Check If Token Expired Using This Jwt Library
Form Builder - Cannot Read Property 'Get' of Undefined, Issue With Validators
How to Insert Space Every 4 Characters for Iban Registering
How to Expire a Cookie in 30 Minutes Using Jquery
Javascript (+) Sign Concatenates Instead of Giving Sum of Variables
Single Quotes in String With Jquery Ajax