Display box at this time on the side - c#

it is such that I am preparing my website for it to appear in the box in a best time by Danish,
this is how it should show example from today and then 2-3 days ahead. and then it must not show box more.
I've tried like this but I keep the error page who do that it will not display the date
string dato = DateTime("01-02-2015");
string datoend = DateTime("03-02-2015");
//skal vise i beste tidspunkt..
if(dato => datoend)
{
PanelMedlemskab.Visible = true;
}
else
{
PanelMedlemskab.Visible = false;
}
The problem is that it in no way want to work with me and I have tried to look at Microsoft dateformat about how to set it up but it just will not as I will when I write the dates on the way.

Try initializating DateTime objects with it's (year, month, day) constructor:
DateTime dato = new DateTime(2015, 2, 1);
DateTime datoend = new DateTime(2015, 2, 3);
PanelMedlemskab.Visible = dato >= datoend;
Or if you are using string, provide exact format for them using DateTime.ParseExact method.

Related

Check if selected date is within three days of a predefined holiday

This is for a program at work. I don't have access to the code right now, but I really want to figure this out, so I wrote a quick program at home that is a mimic of my work project.
I need to know if that date is within 3 working days of a "holiday". If it is, then the selected date must be incremented by one to skip over the holiday. Example:
If there is a holiday on "01/19/2018" and a user selects "01/16/2018", then the program must skip the 19th and then set the closing date as "01/20/2018". Now if I do pure if/then statements, I can get it to work as expected, but the problem then becomes: "what if both 01/19/2018 AND 01/20/2018 are holidays?" This is where I'm stuck.
At work, I'm using a SQL table that already has a column for dates in "MM/dd/yyyy" format (so those dates are my list source that I am checking against.) The user opens a calendar and selects a date. I'm actually parsing an XML file that is created by another method that actually touches the database then using XElement for an iteration that puts all those dates in a string list.)
This is the code in my pseudo work program:
private void button1_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
txtLog.Text = dt.ToString("MM/dd/yy") + Environment.NewLine;
List<string> allexemptdays = new List<string>();
string[] days = { "11/19/2018", "11/20/2018" };
foreach (string a in days)
{
allexemptdays.Add(a);
}
foreach (string a in allexemptdays)
{
ToLog(a);
}
DateTime eq = DateTime.Parse(txtDate.Text);
string thefrustrationisreal = eq.ToString("MM/dd/yy");
if (Math.Abs(DateTime.Now.Subtract(eq).Days) <= 3)
{
MessageBox.Show("Date exemption found.");
}
}
private void ToLog(string a)
{
txtLog.Text += ("Array value to list: " + a + Environment.NewLine);
}
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
txtDate.Text = monthCalendar1.SelectionStart.ToString("MM/dd/yy");
}
Obviously I understand "DateTime.Now" doesn't reflect what I require, but I was just playing more with the DateTime class.
So in short: I'm creating an array with the dates (similar to my real program)
Iterating through them, adding them to a list. Now I need to check if the selected date falls within 3 days of the predefined date from a returned value from my sql table.
If a date is found, move the closing date up a day; however, if holidays in a row are found, then skip those two dates altogether. I can do this on a one by one basis (using if/then) but this doesn't work well if two holidays are back to back of each other.
As a side note, I want to be able to tell if the selected date is within 3 days AND moves over to another month, but I think I can handle that with DateTime, checking the days in a month and incrementing when needed (or I'm sure DateTime could do that for me actually.) I'm not too worried about this concern though at this point.
I did accept Mike's answer as it does work as intended, but! I now have a different issue
Now the "days.count()" holds a persistent number after the user selects a date beyond the holidays. Example: Selecting January 21 2017 returns "Janurary 26 2017" as the closing date because it's still counting the 20th and 21st as holidays affected by the range, when it should just set the closing date now at "Janurary 24th 2017"
I've played around with the code provided; I can't find a good way to fix it. I was thinking something along the lines of:
If(selectedDate.AddDays(3) >= holidaydate) then do a check for another day through the datetime list, but I'd still be making that way more complicated than it should be (I think.)
Last entry I'll do to this post (I'm considering this post solved now):
Matt's answer I selected does work to an extent and I appreciate his help. Problem is, the .Count() never resets, so if you go past the holiday(s) dates, it always holds onto the skipped days (so instead of going to 3 days ahead of the selected date, it'll go (3+the total number of days counted))
In the event someone else comes across a situation like this, here's what I ended up doing:
Downloaded the "WorkingDaysManagement" reference, instructions from:
https://github.com/Opturne/WorkingDaysManagement
the code I ultimately used is:
private void button1_Click(object sender, EventArgs e)
{
txtLog.Text = null;
DateTime dt = DateTime.Now;
var sd = (DateTime.Parse(txtDate.Text));
Console.WriteLine(sd.ToShortDateString());
var listHolidays = new List<DateTime>
{
new DateTime(2017, 12, 24),
new DateTime(2017, 12, 25)
};
var listWeekEnd = new List<DayOfWeek>
{
DayOfWeek.Saturday,
DayOfWeek.Sunday
};
var helper = new WorkingDayHelper(listHolidays,listWeekEnd);
foreach(DateTime s in helper.GetSpanDates(sd,3))
{
Console.WriteLine("Current iteration date: "+s.ToShortDateString());
}
var nextworkday = (helper.GetSpanDates(sd, 3).Last().ToString());
txtLog.Text += ("Next working day available: " + nextworkday + Environment.NewLine);
if(helper.IsWorkingDay(sd)==true)
{
txtLog.Text += ("It is a working a day." + Environment.NewLine);
}
else
{
txtLog.Text += ("NOT a working day!" + Environment.NewLine);
}
txtEndDate.Text = nextworkday;
}
The author of the plugin was gracious enough to put his work under the MIT license as well.
Let's say you have parsed all the date strings from the database into a List<DateTime> namely holidays. Then you can do something like
// Parsed holidays in a list, we will use LINQ against it.
var holidays = new List<DateTime> {
new DateTime(2017, 1, 19),
new DateTime(2017, 1, 20),
};
// Selected Date. In your case it would be: DateTime.Parse(txtDate.Text);
var selectedDate = new DateTime(2017, 1, 16);
// Count how many holidays there are within 3 days from the selcted date.
var count = holidays.Count(holiday => holiday - selectedDate <= TimeSpan.FromDays(3));
// Then add the count and 3 to the selected date
var closingDate = selectedDate.AddDays(count + 3);
// Now we have to make sure the closing date is not a holiday.
if (count > 0)
{
int skipDays = 0;
while (holidays.Any(holiday => holiday == closingDate.AddDays(skipDays)))
{
// holidays list contains the date. Skip it.
skipDays++;
}
closingDate = closingDate.AddDays(skipDays);
}

calling specific DateTime date as an input argument, c#

How do I use a specific date as an input value?
var experiment1 = WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
Later on, I'll be convering it to a string, but I need it to be input as a DateTime.
public static string GetDisplayString(string city, DateTime date, double temp)
{
}
I'm using DateTime.Today as a placeholder, simply because it works. The thing is, I need it input as a specific day of a month of a year. (I've tried using (10, 10, 10) but it simply gives me a compiler error.
Edit.: I can't believe I did not figure out all I need to do is to add "new". Thanks folks
In (10, 10, 10) do you want the year 10?
Use the constructor that takes in a year, a month and a day. As below.
DateTime dt = new DateTime(2013, 12, 12);
then call your method
var experiment1 = WorkingWithDates.GetDisplayString("London", dt, 45.00);
PS: If you want year 10
DateTime dt = new DateTime(10, 10, 10);
will work. The year will be 0010
WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
you need a DateTime object to be used in place of DateTime.Today, so construct your instance using
DateTime newdate = new DateTime(2013,10,10) // year, month , day
and then
WorkingWithDates.GetDisplayString("London", newdate, 45.00);

Determine if a date is a "work day off"

At my work, we are on the 9/80 plan where we get every other Friday off. We've got a small program that shows a DevExpress.Scheduler control and I'd like to color our "Friday's off" a different color. What I need to know is how do I know if a date is one of our Friday's off? The Friday's off will always be every other week (in other words, we don't skip a week due to a holiday or something like that). I have the date of our First Friday off of the year, so I think I can use that somehow...I can also get the date from the scheduler as it's drawn so I have something to compare to.
DateTime dtFirstFridayOff = new DateTime(2011, 1, 1);
DateTime dtCellDate = Convert.ToDateTime(e.Cell.Value);
Now I'm a bit lost as to how to check if dtCellDate is a Friday off.
public static bool IsDateMultipleDays(DateTime originalDate, int numberOfDays, DateTime potentialDate)
{
var original = originalDate.Date; // to make sure that it doesn't have a time portion
var potential = potentialDate.Date;
var difference = potential - original;
return (int)difference.TotalDays % numberOfDays == 0;
}
Then you'd call it like this:
IsDateMultipleDays(dtFirstFridayOff, 14, dtCellDate)
Try this:
bool IsFridayOff(DateTime dt)
{
if (dt.DayOfWeek != DayOfWeek.Friday)
{
return false;
}
DateTime dtFirstFridayOff = new DateTime(2011, 1, 1);
TimeSpan span = dtFirstFridayOff - dt.Date;
return (int) span.TotalDays%14 == 0;
}

Setting DatePicker Value

I currently have a program that takes the value from a datePicker and have the date saved as a string. I only needed the date not the time so i used the following code to save the date value:
DateTime StartDate;
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString()
I have the inMyString pushedBack into my list and now i want to place it back into the datePicker.
On MSDN is shows me the following example to set the date.
dateTimePicker1.Value = new DateTime(2001, 10, 20);
the problem is that having .Value after my date picker is not an option (it doesn't show in Intellisense.)
I have also tried
datePicker1.SelectedDate.Value= new DateTime(inMyString)
and also converting the inMyString to type DateTime but it still does not work.
Any thoughts on how to do this?
Any Suggestions and comments are appreciated.
Thanks!
Try this:
datePicker1.SelectedDate = new DateTime(2001, 10, 20);
If you need to take datetime from string:
datePicker1.SelectedDate = DateTime.Parse(inMyString);
Side note:
You can replace those 3 lines:
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString();
with one:
var inMyString = datePicker1.SelectedDate.Value.ToShortDateString();
Another side note: don't know if there is a reason to it, but you might consider storing datetime as dattime, not as a string.
If you want to show today's date in WPF C#, use this method:
datePicker1.SelectedDate = DateTime.Today.AddDays(-1);

C# Is it possible to convert DateTime format to integer or float?

I'm having some trouble here.
Did some research on Google but I can't seem to find what I'm looking for.
I'm trying to ask for two inputs (datetimes) in hh:mm format, subtract one for the other then return the result of that value in minutes.
The problem is that I want to return that value as an integer and I can't seem to find the right way to do it.
In C/C++ I wouldn't have this kind of issues...
Anyways, here's a snippet of what I'm talking about.
private int DuraçaoTreino(DateTime dtInicioTreino, DateTime dtFimTreino, int dtDuraçao)
{
Console.WriteLine("Introduza a hora de inicio (hh:mm): ");
dtInicioTreino = Convert.ToDateTime(Console.Read());
Console.WriteLine("Introduza a hora de fim (hh:mm): ");
dtFimTreino = Convert.ToDateTime(Console.Read());
dtDuraçao = (dtFimTreino - dtInicioTreino); // duração da sessão de treino
dtDuraçao = Convert.ToDecimal(Console.Read());
return dtDuraçao;
}
And that's pretty much it... I'm new to C# so if you see anything wrong please be kind.
Thanks in advance.
What you're talking about is a TimeSpan:
DateTime dtBegin = new DateTime(2011,5,1,22,0,0) ; // 10pm 1 May 2011
DateTime dtEnd = new DateTime(2011,5,1,23,0,0) ; // 11pm 1 May 2011
TimeSpan tmElapsed = dtEnd - dtBegin ; // tmElapsed is a TimeSpan with a value of 60 minutes
To return the minutes, do something like:
int elapsedTimeInMinutes = (int) Math.Round( tmElapsed.TotalMinutes , 0 ,MidpointRounding.ToEven ) ;
var timeInMinutes = new DateTime(2011, 12, 25).Subtract(new DateTime(2010, 1, 1)).TotalMinutes;
Instead of creating the DateTime objects using the constructor I used, you can use DateTime.Parse, or better still DateTime.ParseExact to convert the strings to date times. (I know I am only using date parts here but you choose only to use time parts if you wish)
Convert DateTime objects to TimeSpan's, substract and call TimeSpan.TotalMinutes (or smth like that - dont' have VS at hand):
DateTime dt1, dt2;
// Assign some dates, then:
TimeSpan ts = dt2 - dt1;
double minutes = ts.TotalMinutes;

Categories

Resources