Check If Datetime Is a Weekend or a Weekday

Check if dateTime is a weekend or a weekday

You wrote wrong varable in the following if statement:

if ((dayToday == DayOfWeek.Saturday) || (dayToday == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}

instead of dayToday you must use day varable in the condition.

UPDATE:
Also you made mistake in condition. There must be or instead of and.

Correct code is

if ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday))
{
Console.WriteLine("This is a weekend");
}

c# check date is weekend or weekday

Use DateTime.ParseExact to parse your string into a DateTime

string dateInput = dateTextbox.Text; //"2016-10-04"
DateTime dtResult = DateTime.ParseExact(dateInput, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DayOfWeek today = dtResult.DayOfWeek;

Check if current time is in weekends Python

You should look at datetime weekday() method which gives you the number of the day in the week.

Basically (if I understand well) you dont want to do something in theses three differents cases:

  1. If the day is a saturday or sunday (it means in day 5 and 6)
  2. If the day is friday (day 4) but the hour is above 23h
  3. If the day is monday (day 0) but the hour is before 1h

So you can do something like this:

import datetime

now = datetime.datetime.now()

if now.weekday() in [5, 6] or (now.weekday() == 4 and now.hour >= 23) or (now.weekday() == 0 and now.hour <= 1):
pass
else:
(do stuff)

I'm pretty sure you can do something shorter and smarter but it do the trick.

how to find current day is weekday or weekends in Python?

You can use the .weekday() method of a datetime.date object

import datetime

weekno = datetime.datetime.today().weekday()

if weekno < 5:
print "Weekday"
else: # 5 Sat, 6 Sun
print "Weekend"

Checking if date is weekend PHP

If you have PHP >= 5.1:

function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}

otherwise:

function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}

How to check if there is a weekend between two dates in php?

You can use date("N") to get the current day of the week and add the difference of days between your dates... If this is bigger or equal to 6 than it's a weekend between or one date is in the weekend.

//Get current day of week. For example Friday = 5
$day_of_week = date("N", strtotime($fromDate));

$days = $day_of_week + (strtotime($toDate) - strtotime($fromDate)) / (60*60*24);
//strtotime(...) - convert a string date to unixtimestamp in seconds.
//The difference between strtotime($toDate) - strtotime($fromDate) is the number of seconds between this 2 dates.
//We divide by (60*60*24) to know the number of days between these 2 dates (60 - seconds, 60 - minutes, 24 - hours)
//After that add the number of days between these 2 dates to a day of the week. So if the first date is Friday and days between these 2 dates are: 3 the sum will be 5+3 = 8 and it's bigger than 6 so we know it's a weekend between.



if($days >= 6){
//we have a weekend. Because day of week of first date + how many days between this 2 dates are greater or equal to 6 (6=Saturday)
} else {
//we don't have a weekend
}

Find if Date is weekend or weekday in Pandas DataFrame

You can first form a dates series from your Year, Month and Day columns:

dates = pd.to_datetime({"year": df.Year, "month": df.Month, "day": df.Day})

then you can check the dayofweek (note the dt accessor before it):

df["Day of Week"] = dates.dt.dayofweek

and form the is_weekend column with your logic:

df["Is Weekend"] = dates.dt.dayofweek > 4

where we don't need apply anymore since we have a unified dates series and it supports this kind of all-at-once comparison.

Check a day to see if it is a weekend day

This will return you 0 to 6, with 0=Sunday, 1=Monday, etc.

$dw = date( "w", $timestamp);

So... to show the notification:

<?php
$dw = date( "w");
if ($dw == 6 || $dw == 0) {
$datetime = new DateTime('today');
if ($dw == 6) {
$datetime->modify('+3 day');
} else {
$datetime->modify('+2 day');
}
echo "Contact us again at: " . $datetime->format('Y-m-d');
} else {
echo "Today is: " . date('l jS \of F Y')."<br/>";
$datetime = new DateTime('today');
$datetime->modify('+1 day');
echo "Tomorrow is: ". $datetime->format('Y-m-d') ."<br/>";
}
?>

C# for loop: Checking if DateTimePicker is weekend or in database

To check if the day is weekend, first, you can refer to this answer to get a list of all dates between two dates. Then use a where clause to filter the list to get all weekends.

DateTime start = dateTimePicker1.Value;
DateTime end = dateTimePicker2.Value;

List<DateTime> weekends = Enumerable.Range(0, 1 + end.Subtract(start).Days)
.Select(offset => start.AddDays(offset))
.Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
.ToList();

When defining a method, you only need to declare its formal parameters. As for "DateTimePicker.Value", it is used as the actual parameter when calling the method.

// Just define the method like this, use a formal parameter "dt"
public static bool isDateInDatabaseAppointmentTable(DateTime dt)

If the type of field date in the database is date, you need to convert dateTimePicker.Value to Date.

In addition, in order to prevent sql injection, using parameters is a better choice.

public static bool isDateInDatabaseAppointmentTable(DateTime dt)
{
string connSQL = @"connection string";
using (SqlConnection conn = new SqlConnection(connSQL))
{
string strSQL = "select count(*) from TableAppointment WHERE AppointmentDate = CAST(@date AS DATE)";
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.Parameters.AddWithValue("@date", dt.ToShortDateString());
conn.Open();
int rows = (int)cmd.ExecuteScalar();
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
}


Related Topics



Leave a reply



Submit