Days remaining from start date is showing incorrectly - c#

I have a repeater that displays an item that a user wants to sell. In one columns of the repeater, the CreateDate or the date the item was posted is displayed like so:
//lblExp.Text originally contains 2013-05-24 14:24:08.000
Label lblExp = (Label)e.Item.FindControl("lblExp");
DateTime StartDate = Convert.ToDateTime(lblExp.Text);
DateTime expDate = StartDate.AddDays(30);
int DaysLeft = (expDate - StartDate).Days;
lblExp.Text = DaysLeft.ToString();
When that code executes I get 30 instead of 28.

Is this what you're trying to say? (replacing StartDate with DateTime.Now)
//lblExp.Text originally contains 2013-05-24 14:24:08.000
Label lblExp = (Label)e.Item.FindControl("lblExp");
DateTime StartDate = Convert.ToDateTime(lblExp.Text);
DateTime expDate = StartDate.AddDays(30);
int DaysLeft = (expDate - DateTime.Now).Days; // Replaced here
lblExp.Text = DaysLeft.ToString();

Related

How to select current Date in Selenium c#

I have calendar that need to select current date I have code but every time new month start then my code selects next day not current day. Thing is on my UI default day is next day shown.
IList<IWebElement> datePicker = driver.FindElements(By.Xpath("element"));
foreach(IWebElement td in datePicker)
{
string date = td.Text;
DateTime currentDate = DateTime.Now;
var todayDate = currentDate.Date.ToString("d");
if(date.Equals(todayDate))
{
td.Click;
break;
}
}

Calculating the Number of Days between two Dates and displaying it in a Label

Hi I'm trying to capture two dates selected by the user in a C# Calendar Control and I want the date range to be displayed in a label. I have worked out on the following code but it generates a Minus value ; not the actual date range.
DateTime from = CalFrom.SelectedDate;
DateTime to = CalTo.SelectedDate;
double days = (CalTo.SelectedDate - CalFrom.SelectedDate).TotalDays;
TimeSpan t = to - from;
double noOfDays = t.TotalDays;
TimeSpan ts = to - from;
double differnceindays = ts.TotalDays;
lblNoofDays.Text = differnceindays.ToString();
This code is working perfectly for me for calculating the number the days between two days.
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(10);
TimeSpan difference = d2 - d1;
var days = difference.TotalDays;
DateTime.Now.Subtract(startDate).Days.ToString();
try to calculate no of days between two dates
string days = (date2 - date1).Value.Days.ToString();
The only problem I see is that you assume the start and end dates will be correctly range checked, meaning start date is never greater than end date (which would produce negative values for total days). If you want to correct for the fact that start date may be after end date, then this should work.
DateTime startDate = DateTime.Now.AddDays(-94); // Example random 94 day span..
DateTime endDate = DateTime.Now;
TimeSpan duration = endDate > startDate ? endDate - startDate : startDate - endDate;
double daysBetweenDates = duration.TotalDays;
Note: "daysBetweenDates" will include fractional days (thus the double type). Also, the code above assumes local time. If you want UTC you will need to account for that.

Date is not incrementing in WHILE or FOR loop

What's wrong with the following code? The date is not incrementing in my FOR loop so it goes into an endless loop because the condition is never met. I also tried it with a WHILE loop and got the same result.
var startDate = DateTime.Today;
var endDate = new DateTime(2016, 12, 31);
for (var date = startDate; date <= endDate; date.AddDays(1))
{
// Some logic here
}
AddDays doesn't modifies value of date, it just returns new instance of DateTime, and you're not assigning back incremented value.
It should be
for (var date = startDate; date <= endDate; date = date.AddDays(1))
From the documentation:
Returns a new DateTime that adds the specified number of days to the
value of this instance.
So, date.AddDays(1) doesn't change the value of date; it returns a new DateTime representing the changed value. If you want to change date, do this:
date = date.AddDays(1);

Remove hours:minutes:seconds from System.DateTime.Now

I want to insert just year : month : day for a specific column in a database table row, but my variable is of DateTime Type.
To remove milliseconds I just used following code:
DateTime createdate = System.DateTime.Now;
createdate = createdate.AddTicks(-(createdate.Ticks % TimeSpan.TicksPerSecond));
How to remove hours, minutes, seconds from this?
You can use .Date property which set's the time part to midnight.
DateTime justDate = createdate.Date;
or you can use DateTime.Today which generates the same result for DateTime.Now.Date value.
DateTime justDate = DateTime.Today;
If you use SQL Server, date type is mapped with DateTime on CLR side which you can safely insert that value.
Use the Date property on your createdate variable to return just the date component without the time component.
DateTime createdate = System.DateTime.Now;
createdate = createdate.AddTicks(-(createdate.Ticks % TimeSpan.TicksPerSecond));
var dateOnly = createdate.Date;
Note you will still have the time in the object, set to 00:00:00

Format text in a Masked TextBox

I have a masked text box that is used to show today's date, like this:
txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
However it can still be edited to change the date. Being a masked text box, the only worry is if someone wants to add the date like so:
27/1/15
Is there a way I can edit this to add the additional information and automatically format it to this? Possibly using String.Format perhaps?
27/01/2015
EDIT: To clarify, When the form opens, the txtDate will automatically receive today's date, but if a user wants to change the date, how can I ensure that the date will remain in the right format? I.e. dd/MM/yyyy.
try something like this
DateTime d = DateTime.Now;
string str = String.Format("{0:00}/{1:00}/{2:0000}", d.Month, d.Day, d.Year);
Edit:
You can add days, months, years... to your variable of type DateTime d
Example:
d = DateTime.Now;
d = d.AddDays(5); // add 5 days to current date
EDIT2
Say you have 3 textBoxes txtDays, txtMonth and txtYear
int year = Convert.ToInt32(txtYear.Text);
int month = Convert.ToInt32(txtMonth.Text);
int days = Convert.ToInt32(txtDays.Text);
// you can assign any date as follows:
DateTime d = new DateTime(year, month, days);
Or if the users will have only one textBox called txtDate and they have to enter the date in the form "MM/dd/yyyy":
var[] date = txtDate.Split('/');
txtDate.Text = String.Format("{0:00}/{1:00}/{2:0000}", date[0], date[1], date[2]);

Categories

Resources