DateTime Custom Formatting for Seasons - c#

I've come across a problem customizing my DateTimePicker value in my application. I've read all about the various formatting strings which you can use to customize the way the date/time is interpreted. The issue is that I actually want some of the text to be ignored in the custom format string so that I can add in the season as a string to the beginning of the DateTimePicker.
For example, let's take today's date which is August 7th, 2013 at 5:30PM (in the US). If I use the custom format string "MMM.d -h:mm tt" then the date will be shown as Aug. 7th - 5:30PM. So, that's perfect. Only, I want to add the season to the beginning of the string. So, in this case, it would be "Summer: Aug. 7th - 5:30PM".
The issue that I'm having is that if I insert the word "Summer" at the beginning of the custom format string, then it actually interprets the double mm's as GetMinute value of the dateTime. I'd like for the season to remain literal, but the rest of the format string to be interpreted (if that makes sense).
Here is the code I'm using:
public Form1()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
season = getSeason(dateTimePicker1.Value);
dateTimePicker1.CustomFormat = convertSeason(season) + " : " + dt_format;
}
public int season = 1; //set default to summer
public string dt_format = "MMM.d -h:mm tt";
private int getSeason(DateTime date)
{
float value = (float)date.Month + date.Day / 100; // <month>.<day(2 digit)>
if (value < 3.21 || value >= 12.22) return 3; // Winter
if (value < 6.21) return 0; // Spring
if (value < 9.23) return 1; // Summer
return 2; // Autumn
}
private string convertSeason(int value)
{
string season = "Spring";
if (value == 1) season = "Summer";
else if (value == 2) season = "Autumn";
else if (value == 3) season = "Winter";
return season;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
season = getSeason(dateTimePicker1.Value);
dateTimePicker1.CustomFormat = convertSeason(season) + " : " + dt_format;
}

You need to surround it in a literal string delimiter (for DateTime format strings): '.
So now your method could be this:
private string convertSeason(int value)
{
string season = "'Spring'";
if (value == 1) season = "'Summer'";
else if (value == 2) season = "'Autumn'";
else if (value == 3) season = "'Winter'";
return season;
}
However, your methods could be improved a bit. So I took the liberty of doing that:
private int GetSeason(DateTime date)
{
//using decimal to avoid any inaccuracy issues
decimal value = date.Month + date.Day / 100M; // <month>.<day(2 digit)>
if (value < 3.21 || value >= 12.22) return 3; // Winter
if (value < 6.21) return 0; // Spring
if (value < 9.23) return 1; // Summer
return 2; // Autumn
}
private string ConvertSeason(int value)
{
switch (value)
{
case 0:
return "'Spring'";
case 1:
return "'Summer'";
case 2:
return "'Autumn'";
case 3:
return "'Winter'";
}
return "";
}

Related

Creating and using Constructors in C# along with Boolean methods

I am having some trouble with constructors in a C# assignment. The first block of code below contains some of the instructions for the assignment. Essentially, as I understand it, I am to create two constructors, one for setting a default date, and another for checking a date from the user. Also, there is a SetDate method that seems to be doing the same thing. These seem redundant, yet according to the assignment instructions, both are required. I am VERY new to object-oriented programming so I am not sure how to "pass" stuff to a constructor or really how to use it and call it in the main method. The second block of code is what I have written so far. All of the date validation methods seem fine. But, I have no idea what to do with the public Date(int M, int D, int Y) constructor and the SetDate method. What should each of these be doing? Also, why am I being instructed to use integer variables M, D, Y when I am also being told to declare Month, Day, and Year above? Any insight that might aid me in understanding how to use this Constructer and how it relates and differs in function from the SetDate method would be greatly appreciated.
//Create a Date Class
//This class holds:
private int Month;
private int Day;
private int Year;
//Include the following constructors/methods. Include others/more if you
//need them.
// Sets date to 1/1/1900
public Date()
// Sets date to user’s input.
// Checks to see the date is valid
// If it isn’t valid, print message and set date to 1/1/1900
public Date(int M, int D, int Y)
// Sets date to user’s input.
// Checks to see the date is valid
// If it isn’t valid, print message and set date to 1/1/1900
public Boolean SetDate(int M, int D, int Y)ere
//******************************************************************************
class Date
{
private int Month;
private int Day;
private int Year;
// Sets date to 1/1/1900
public Date()
{
Month = 1;
Day = 1;
Year = 1900;
}
public Date(int M, int D, int Y)
{
Month = M;
Day = D;
Year = Y;
}
public Boolean SetDate(int M, int D, int Y)
{
int valDate = 0;
Console.WriteLine("You will be prompted to enter three(3) numbers to represent a month, " +
"day, and year. Only dates between 1/1/1900 and 12/31/2100 are valid.");
Console.WriteLine("");
while (valDate < 1)
{
Console.WriteLine("Enter the number for the month.");
M = int.Parse(Console.ReadLine());
Console.WriteLine("");
Console.WriteLine("Enter the number for the day.");
D = int.Parse(Console.ReadLine());
Console.WriteLine("");
Console.WriteLine("Enter the number for the year.");
Y = int.Parse(Console.ReadLine());
Console.WriteLine("");
ValidateDate();
if (ValidateDate())
{
DisplayDate();
valDate++;
return true;
}
else
{
Console.WriteLine("Please enter a valid date.");
Console.WriteLine("");
Month = 1;
Day = 1;
Year = 1900;
return false;
}
}
return false;
}
// Determines if date is valid.
public Boolean ValidateDate()
{
ValidateMonth();
ValidateDay();
ValidateYear();
if (ValidateMonth() && ValidateDay() && ValidateYear())
{
return true;
}
else
{
return false;
}
}
// Determines if month is valid.
public Boolean ValidateMonth()
{
if (Month >= 1 && Month <= 12)
{
return true;
}
else
{
return false;
}
}
// Determines if year is valid.
public Boolean ValidateYear()
{
if(Year >= 1900 && Year <= 2100)
{
return true;
}
else
{
return false;
}
}
// Determines if day is valid
public Boolean ValidateDay()
{
IsLeapYear();
if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
{
if (Day >= 1 && Day <= 31)
{
return true;
}
else
{
return false;
}
}
else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
{
if (Day >= 1 && Day <= 30)
{
return true;
}
else
{
return false;
}
}
else if (Month == 2 && IsLeapYear())
{
if (Day >= 1 && Day <= 29)
{
return true;
}
else
{
return false;
}
}
else if (Month == 2 && !IsLeapYear())
{
if (Day >= 1 && Day <= 28)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Determine if year is a leap year
public Boolean IsLeapYear()
{
if ((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
// Print date to screen in format M/D/Y
public void DisplayDate()
{
Console.WriteLine(ShowDate());
}
public String ShowDate()
{
StringBuilder myStringBuilder = new StringBuilder();
myStringBuilder.AppendFormat("{0} / {1} / {2}", Month, Day, Year);
return (myStringBuilder.ToString());
}
static void Main(string[] args)
{
Date NewDate = new Date();
NewDate.Date();
Console.ReadLine();
}
}
It looks like you've been asked to create a method and a constructor that are doing the same thing. The simple thing to do in this case is to have the constructor call the method.
The only comment I would make about your code is that the problem statement you showed did not require the input to be gathered in the SetDate method. Given the statement it seems like the input from the user would be gathered outside your class.
I don't know what your requirements are for the failure message. That might make sense in it's own method as well.
Here is an example:
public class Date
{
private int Month;
private int Day;
private int Year;
public Date()
{
SetDefaultDate();
}
public Date(int M, int D, int Y)
{
SetDate(M, D, Y);
}
public void SetDate(int M, int D, int Y)
{
if (IsValidDate(M, D, Y))
{
Month = M;
Day = D;
Year = Y;
}
else
{
SetDefaultDate();
}
}
private bool IsValidDate(int M, int D, int Y)
{
// validation logic.. return true if all parameters result in valid date
// false if they do not. If it is an invalid date print the failure message.
return true;
}
private void SetDefaultDate()
{
Month = 1;
Day = 1;
Year = 1900;
}
}
If you want to make some real application using date then i'd suggesting taking look at the DateTime struct in System namespace. That has TryParse function which will return you true or false whether if the input was valid or not.
However it seems like you're doing some programming exercise so in that case my answer would be: It's not very good to have arguments in constructor which could lead to an invalid object - in your case invalid date. That's because once calling the constructor you will have an object in one way or another, except if you throw exception in constructor. Yet if you still want to have it like that then you need to have a property or public variable called "IsValid" in the class/struct which tells if valid date was given with constructor.
Nicer option is follow the DateTime approach - have a public static function which return valid Date object. Like this:
public bool TryParse(int m, int d, int y, out Date date)
{
// validate
// if valid then return Date object like that:
date = new Date()
{
Month = m,
Day = d,
Year = y
};
return true;
// Or like that:
date = new Date(m, d, y);
return true;
// if not valid then return null (because have to return something)
date = null;
return false;
}

Compare Date is Greater than in C#

I'm trying to check if the passing variable date is greater than a static date which I have included in the code and currently trying to use the following code,
private String LastPayDate {
get {
string foo;
if(Parameters.TryGetValue("Last Pay Date", out foo))
return foo;
else
return null;
}
}
private Boolean IsLastPay() {
if (!string.IsNullOrEmpty(LastPayDate)) {
if(DateTime.Parse(Parameters.TryGetValue("Last Pay Date") >="24/05/2018")
return true;
else
return false;
}
return false;
}
however the only error I get is within below code section,
if(DateTime.Parse(Parameters.TryGetValue("Last Pay Date") > "24/05/2018")
can anyone help please ?
If you want to compare DateTimes, compare them, but not strings:
//TODO: what is the magic number (date) 24 May 2018?
private Boolean IsLastPay() {
if (Parameters.TryGetValue("Last Pay Date", out var dateSt))
if (DateTime.TryParse(dateSt, out var paramDate))
return paramDate >= new DateTime(2018, 5, 24);
else
return false; // DateTime.TryParse failed to parse the parameter
else
return false; // Parameters.TryGetValue failed to get the value
}
Thank you for respond. It did helped and I've managed to use below code and its working now, Much appreciate Help!
private Boolean IsLastPay()
{
if (!string.IsNullOrEmpty(LastPayDate))
{
string lpd;
}
if(Parameters.TryGetValue("Last Pay Date", out lpd))
{
if(DateTime.Parse(lpd) > new DateTime(2018,05,24))
return true;
else
return false;
}
}
return false;
}
Why not use the DateTime.Compare() method of DateTime class.
For this you need to have both the variables/objects of type DateTime.
string staticDate = "24/05/2018"; //dd-MM-yyyy
string inputDate = "14/08/20"; //dd-MM-yy
string greaterDate = CalculateGreaterDate(inputDate, staticDate); // 14/08/20 is greater
public static string CalculateGreaterDate(string iDate, string sDate)
{
// input date
string input = iDate;
var inputElements = input.Split('/');
int inputDay = Convert.ToInt32(inputElements[0]); //14
int inputMonth = Convert.ToInt32(inputElements[1]); //08
int inputYear = Convert.ToInt32(inputElements[2]); //20
// static date
string static = sDate;
var staticElements = static.Split('/');
int staticDay = Convert.ToInt32(staticElements[0]); //24
int staticMonth = Convert.ToInt32(staticElements[1]); //05
int staticYear = Convert.ToInt32(staticElements[2]); //2018
DateTime inputDate = new DateTime(inputYear, inputMonth, inputDay);
DateTime staticDate = new DateTime(staticYear, staticMonth, staticDay);
// DateTime.Compare(d1, d2) returns:
// > 0 : d1 is greater than d2
// = 0 : d1 & d2 are equal
// < 0 : d1 is smaller than d2
int result = DateTime.Compare(inputDate, staticDate);
if (result > 0)
return iDate + " is greater";
else if (result < 0)
return sDate + " is greater";
else if (result == 0)
return iDate + " is equal to " + sDate;
}

Regex c# - where to insert the blank space

I want to allow the user to enter the year in blank but I don´t know where to put \s\s\s\s in the following expression.
Here is an example of what I need to do: if the user inserts 03-07-_____ the program must be executed every 3Th of July of each year(and it proceeds the same way if the user inserts blank date, month or year or twoo of this three)
System.Text.RegularExpressions.Regex rdate =
new System.Text.RegularExpressions.Regex(#"^((((0?[1-9]|[12]\d|3[01]|\s\s|\s\d)[\-](0?[13578]|1[02]|\s\s)[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\-](0?[13456789]|1[012])[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\-]0?2[\-]((1[6-9]|[2-9]\d)?\d{2}))|(29[\-]0?2[\-]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$");
Could someone help me?(this expression validates even a leap year)
Here is what You asked for
System.Text.RegularExpressions.Regex rdate =
new System.Text.RegularExpressions.Regex(#"^((((0?[1-9]|[12]\d|3[01]|\s\s|\s\d)[\-](0?[13578]|1[02]|\s\s)[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|((0?[1-9]|[12]\d|30)[\-](0?[13456789]|1[012])[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|((0?[1-9]|1\d|2[0-8])[\-]0?2[\-]((1[6-9]|[2-9]\d)?\d{2}|\s\s\s\s))|(29[\-]0?2[\-]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|\s\s\s\s)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$");
But I am not sure it is what You really want. The regex was already patched, I continued the way. But not only it is maintenance nightmare, it behaves strange. It accepts empty day, but only when month is january, march, may... or empty. Less lines of code is not always better. I would suggest to rewrite it completely. Something simmilar to this:
protected DateTime? getDateTimeFromParts(string day, string month, string year)
{
DateTime now = DateTime.Now;
int iyear;
if (string.IsNullOrWhiteSpace(year))
{
iyear = now.Year;
}
else
{
iyear = int.Parse(year);
if (iyear >= 0 && iyear < 100) { iyear += 2000; }
}
int imonth;
if (string.IsNullOrWhiteSpace(month))
{
imonth = now.Month;
}
else
{
imonth = int.Parse(month);
}
int iday;
if (string.IsNullOrWhiteSpace(day))
{
iday = now.Day;
}
else
{
iday = int.Parse(day);
}
if (iyear <= DateTime.MaxValue.Year && iyear >= DateTime.MinValue.Year)
{
if (imonth >= 1 && imonth <= 12)
{
if (DateTime.DaysInMonth(iyear, imonth) >= iday && iday >= 1)
return new DateTime(iyear, imonth, iday);
}
}
return null;
}
protected DateTime? getDateTime(string dateStr)
{
Regex r = new Regex(#"^(\d\d)(\d\d)((\d\d)?\d\d)$");
Match m = r.Match(dateStr);
if (m.Success)
{
return getDateTimeFromParts(m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
}
r = new Regex(#"^(\d?\d|\s\d|\s\s)[-](\d?\d|\s\s)[-]((\d\d)?\d\d|\s\s\s\s)$");
m = r.Match(dateStr);
if (m.Success)
{
return getDateTimeFromParts(m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
}
return null;
}

Convert.ToDateTime DD:HH:mm:ss C#/ASP.net GridView RowDataBound

The basis of my question is to color a cell in a ASP.net Grid View control.
I have a bound field that is produced from this in SQL
RIGHT('0' + CONVERT(varchar(6), SUM([Coaching]) / 86400), 2) + ':' + RIGHT('0' + CONVERT(varchar(6), SUM([Coaching]) % 86400 / 3600), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), SUM([Coaching]) % 3600 / 60), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), SUM([Coaching]) % 60), 2)
It is not great for working with I know but the Project rules that I have says I have to have in a format of DD:HH:MM:SS for the display.
All that being said I'm trying to do a row data bound event in C# to color the cell if it is over a certain Minute
protected void TheGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//this is where we will color the columns
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CoachingIndex = GetColumnIndexByName(e.Row, "Coaching");
DateTime CoachingValue = Convert.ToDateTime(e.Row.Cells[CoachingIndex].Text);
string columnValue = ((Label)e.Row.FindControl("Coaching")).Text;
if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00"))
{
e.Row.Cells[CoachingIndex].BackColor = System.Drawing.Color.Red;
}
}
}
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
This conversion if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00")) is where the problem lies just trying to figure out how to get this to work in C# so I can do the comparison.
Any help will be appreciated.
Thank you for all the help I was able to work on it today after all the info provided was able to get it to work.
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CoachingIndex = GetColumnIndexByName(e.Row, "Coaching");
TimeSpan timeStamp;
string timeStampString = e.Row.Cells[CoachingIndex].Text; // just change the index of cells to get the correct timestamp field
if (TimeSpan.TryParse(timeStampString, out timeStamp))
{
TimeSpan TS = timeStamp;
int mins = TS.Minutes;
int hours = TS.Hours;
int days = TS.Days;
if (mins > 40 || hours >= 1 || days >= 1)
{
e.Row.Cells[CoachingIndex].BackColor = System.Drawing.Color.Red;
}
}
}
}
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
Try using a common safe function like that while parsing the date:
public DateTime? GetSafeDateTimeValue(object datetimeValue)
{
if (datetimeValue == null || datetimeValue == DBNull.Value)
return null;
else
{
try
{
if (!DateTime.TryParse(datetimeValue.ToString(), out tempDateTimeValue))
return null;
else
return tempDateTimeValue;
}
catch
{
return null;
}
}
}
And replace your line with
DateTime? dt = GetSafeDateTimeValue((DataBinder.Eval(e.Row.DataItem, columnValue)));
if(dt.HasValue)
if (dt.Value > Convert.ToDateTime("00:00:40:00"))
Second problem you will face that the date time parser must expect the year, instead of parsing the datetime string take datetime instance like below:
DateTime comparedDt = new DateTime(2014, 03, 10, 00, 40, 00);

Adding Days to a Date but Excluding Weekends

Given a date how can I add a number of days to it, but exclude weekends. For example, given 11/12/2008 (Wednesday) and adding five will result in 11/19/2008 (Wednesday) rather than 11/17/2008 (Monday).
I can think of a simple solution like looping through each day to add and checking to see if it is a weekend, but I'd like to see if there is something more elegant. I'd also be interested in any F# solution.
using Fluent DateTime https://github.com/FluentDateTime/FluentDateTime
var dateTime = DateTime.Now.AddBusinessDays(4);
public DateTime AddBusinessDays(DateTime dt, int nDays)
{
int weeks = nDays / 5;
nDays %= 5;
while(dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
dt = dt.AddDays(1);
while (nDays-- > 0)
{
dt = dt.AddDays(1);
if (dt.DayOfWeek == DayOfWeek.Saturday)
dt = dt.AddDays(2);
}
return dt.AddDays(weeks*7);
}
Without over-complicating the algorithm, you could just create an extension method like this:
public static DateTime AddWorkingDays(this DateTime date, int daysToAdd)
{
while (daysToAdd > 0)
{
date = date.AddDays(1);
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
daysToAdd -= 1;
}
}
return date;
}
I would use this extension, remember since it is an extension method to put it in a static class.
Usage:
var dateTime = DateTime.Now.AddBusinessDays(5);
Code:
namespace ExtensionMethods
{
public static class MyExtensionMethods
{
public static DateTime AddBusinessDays(this DateTime current, int days)
{
var sign = Math.Sign(days);
var unsignedDays = Math.Abs(days);
for (var i = 0; i < unsignedDays; i++)
{
do
{
current = current.AddDays(sign);
} while (current.DayOfWeek == DayOfWeek.Saturday ||
current.DayOfWeek == DayOfWeek.Sunday);
}
return current;
}
}
}
Source:
https://github.com/FluentDateTime/FluentDateTime/blob/master/src/FluentDateTime/DateTime/DateTimeExtensions.cs
int daysToAdd = weekDaysToAdd + ((weekDaysToAdd / 5) * 2) + (((origDate.DOW + (weekDaysToAdd % 5)) >= 5) ? 2 : 0);
To wit; the number of "real" days to add is the number of weekdays you're specifying, plus the number of complete weeks that are in that total (hence the weekDaysToAdd / 5) times two (two days in the weekend); plus a potential offset of two days if the original day of the week plus the number of weekdays to add "within" the week (hence the weekDaysToAdd mod 5) is greater than or equal to 5 (i.e. is a weekend day).
Note: this works assuming that 0 = Monday, 2 = Tuesday, ... 6 = Sunday. Also; this does not work on negative weekday intervals.
I created an extension that allows you to add or subtract business days.
Use a negative number of businessDays to subtract. It seems to work in all cases.
namespace Extensions.DateTime
{
public static class BusinessDays
{
public static System.DateTime AddBusinessDays(this System.DateTime source, int businessDays)
{
var dayOfWeek = businessDays < 0
? ((int)source.DayOfWeek - 12) % 7
: ((int)source.DayOfWeek + 6) % 7;
switch (dayOfWeek)
{
case 6:
businessDays--;
break;
case -6:
businessDays++;
break;
}
return source.AddDays(businessDays + ((businessDays + dayOfWeek) / 5) * 2);
}
}
}
Example:
using System;
using System.Windows.Forms;
using Extensions.DateTime;
namespace AddBusinessDaysTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = DateTime.Now.AddBusinessDays(5).ToString();
label2.Text = DateTime.Now.AddBusinessDays(-36).ToString();
}
}
}
F# flavor of http://stackoverflow.com/questions/1044688 's answer:
namespace FSharpBasics
module BusinessDays =
open System;
let private weekLength = 5
(*operation*)
let addBusinessDays (numberOfBusinessDays: int) (startDate: DateTime) =
let startWeekDay = startDate.DayOfWeek
let sign = Math.Sign(numberOfBusinessDays)
let weekendSlide, businessDaysSlide =
match startWeekDay with
| DayOfWeek.Saturday when sign > 0 -> (2, -1)
| DayOfWeek.Saturday when sign < 0 -> (-1, 1)
| DayOfWeek.Sunday when sign > 0 -> (1, -1)
| DayOfWeek.Sunday when sign < 0 -> (-2, 1)
| _ -> (0, 0)
let baseStartDate = startDate.AddDays (float weekendSlide)
let days = Math.Abs (numberOfBusinessDays + businessDaysSlide) % weekLength
let weeks = Math.Abs (numberOfBusinessDays + businessDaysSlide) / weekLength
let baseWeekDay = int baseStartDate.DayOfWeek
let oneMoreWeekend =
if sign = 1 && days + baseWeekDay > 5 || sign = -1 && days >= baseWeekDay then 2
else 0
let totalDays = (weeks * 7) + days + oneMoreWeekend
baseStartDate.AddDays (float totalDays)
[<EntryPoint>]
let main argv =
let now = DateTime.Now
printfn "Now is %A" now
printfn "13 business days from now would be %A" (addBusinessDays 13 now)
System.Console.ReadLine() |> ignore
0
This is better if anyone is looking for a TSQL solution. One line of code and works with negatives.
CREATE FUNCTION[dbo].[AddBusinessDays](#Date date,#n INT)RETURNS DATE AS BEGIN
DECLARE #d INT;SET #d=4-SIGN(#n)*(4-DATEPART(DW,#Date));
RETURN DATEADD(D,#n+((ABS(#n)+#d-2)/5)*2*SIGN(#n)-#d/7,#Date)END
Here is how I did it.
I had to calculate SLA (Service Level Agreement) due dates based on a start date and number of days, and account for weekends and public holidays:
public DateTime? CalculateSLADueDate(DateTime slaStartDateUTC, double slaDays)
{
if (slaDays < 0)
{
return null;
}
var dayCount = slaDays;
var dueDate = slaStartDateUTC;
var blPublicHoliday = new PublicHoliday();
IList<BusObj.PublicHoliday> publicHolidays = blPublicHoliday.SelectAll();
do
{
dueDate = dueDate.AddDays(1);
if ((dueDate.DayOfWeek != DayOfWeek.Saturday)
&& (dueDate.DayOfWeek != DayOfWeek.Sunday)
&& !publicHolidays.Any(x => x.HolidayDate == dueDate.Date))
{
dayCount--;
}
}
while (dayCount > 0);
return dueDate;
}
blPublicHoliday.SelectAll() is a cached in-memory list of public holidays.
(note: this is a cut down version for sharing publicly, there is a reason its not an extension method)
enter code public static DateTime AddWorkDays(DateTime dt,int daysToAdd)
{
int temp = daysToAdd;
DateTime endDateOri = dt.AddDays(daysToAdd);
while (temp !=0)
{
if ((dt.AddDays(temp).DayOfWeek == DayOfWeek.Saturday)|| (dt.AddDays(temp).DayOfWeek == DayOfWeek.Sunday))
{
daysToAdd++;
temp--;
}
else
{
temp--;
}
}
while (endDateOri.AddDays(temp) != dt.AddDays(daysToAdd))
{
if ((dt.AddDays(temp).DayOfWeek == DayOfWeek.Saturday) || (dt.AddDays(temp).DayOfWeek == DayOfWeek.Sunday))
{
daysToAdd++;
}
temp++;
}
// final enddate check
if (dt.AddDays(daysToAdd).DayOfWeek == DayOfWeek.Saturday)
{
daysToAdd = daysToAdd + 2;
}
else if (dt.AddDays(daysToAdd).DayOfWeek == DayOfWeek.Sunday)
{
daysToAdd++;
}
return dt.AddDays(daysToAdd);
}
DateTime oDate2 = DateTime.Now;
int days = 8;
for(int i = 1; i <= days; i++)
{
if (oDate.DayOfWeek == DayOfWeek.Saturday)
{
oDate = oDate.AddDays(2);
}
if (oDate.DayOfWeek == DayOfWeek.Sunday)
{
oDate = oDate.AddDays(1);
}
oDate = oDate.AddDays(1);
}
Given the number of the original day in the year D and original day in the week W and the number of workdays to add N, the next weekday number is
W + N % 5.
The next day in the year (with no wraparound check) is
D + ((N / 5) * 7) + N % 5).
This is assuming that you have integer division.
Formula will be: Workday(date,no.of days,(weekday(1)))
Try this. This will help.

Categories

Resources