I am working on getting the date validated before we insert them in to the mySQl server. Below is what I am trying
DateTime startDate = new DateTime(2009, 12, 31);
var currentTime = DateTime.Now;
DateTime endDate = currentTime.Date;
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
if (!(DOB.Date > startDate && DOB.Date <= endDate))
{
WriteValidationFailure("Failed - DOB is Invalid");
}
Here emp[i].DateOfBirth is a string and hold the value like 01/01/2009. When I run the application it throws
You need to use one of these statements to parse your date:
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
or
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
First if your dates contains month as first part and second for days as first part.
Related
I have a start date and end date during a week, and I have to subtract each day that passes during the week. As each day goes by I want to display 6 days left, 5 days left, etc.
I tried this:
DateTime endDate = DateTime.Now.AddDays(-1);
if (endDate == startDate)
{
// do something
}
But it's not working.
I believe the user is asking for time left between now and a specific date.
DateTime dateTime = new DateTime(2019, 12, 25);
TimeSpan timeLeft = dateTime - DateTime.Now; // At time of posting, it is 18/12/2019
Console.WriteLine($"{timeLeft.Days} days left");
Because you are always subtracting from DateTime.Now (DateTime endDate = DateTime.Now.AddDays(-1);)
DateTime endDate = DateTime.Today;
DateTime startDate = DateTime.Today.AddDays(-7);
do
{
enddate = enddate.AddDays(-1);
}while(enddate>=startdate);
how can I calculate if a date (in DateTime format) is 6 month later or not from my BirthDate (in DateTime format)?
Use DateTime AddMonth method
https://msdn.microsoft.com/ru-ru/library/system.datetime.addmonths(v=vs.110).aspx
var dat = new DateTime(2015, 12, 31);
var dat2 = new DateTime(2015, 12, 31);
if (dat.AddMonths(6) < dat2) { ... }
You should use DateTime.AddMonths :
DateTime dt;
DateTime birthDate;
if (dt <= birthDate.AddMonths(6))
{
}
enter your birth date, calculate your next birthday and compare the dates,
var born = new DateTime(1900, 02, 01);
var checkdate = DateTime.Now;
var nextBirthday = new DateTime(DateTime.Now.Year, born.Month, born.Day);
if (nextBirthday < DateTime.Now)
{
nextBirthday = new DateTime(DateTime.Now.Year + 1, born.Month, born.Day);
}
if (checkdate.AddMonths(6) < nextBirthday)
{
Console.WriteLine("date is 6 months later then birthday");
}
else
{
Console.WriteLine("wait for it");
}
DateTime birthDate=new DateTime(year,month,day);
DateTime dateToCompare = new DateTime(year, month, day);
if(dateToCompare >= birthdate.AddMonths(6))
{
//DoSomething
}
You could calculte the difference between dates using Subtract method and calculate how many months you have between these dates, for sample:
DateTime birthDay = /* some date */;
DateTime someDate = /* some date */;
var months = someDate.Subtract(birthDay).Days / (365.25 / 12);
This answer provides a good helper for Dates:
https://stackoverflow.com/a/33287670/316799
Assuming I can not change service that returns data, I am left with
var date = "20140231";
var scope = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture);
Clearly "20140231" is lazy way of saying end of February. What is the cleanest way to get last date of February with input of "20140231"?
There is 1 constraint - this should work with .net 2.0.
string date = "20140231";
DateTime result;
int year = Convert.ToInt32(date.Substring(0, 4));
int month = Convert.ToInt32(date.Substring(4, 2));
int day = Convert.ToInt32(date.Substring(6, 2));
result = new DateTime(year, month, Math.Min(DateTime.DaysInMonth(year, month), day));
February can have only 28 or 29 days depends on current year is a leap year or not.
It can't have 30 or 31 days in any year. That's why you can't parse your 20140231 string successfully.
You can clearly get the last day of February like;
DateTime lastDayOfFebruary = (new DateTime(2014, 2, 1)).AddMonths(1).AddDays(-1);
If your service always get year as a first 4 character, you can use .Substring() to get year and pass DateTime constructor as a year.
var date = "20140231";
string year = date.Substring(0, 4);
DateTime lastDayOfFebruary = (new DateTime(int.Parse(year), 2, 1)).AddMonths(1).AddDays(-1);
You could create a while, cut the date in pieces, and keep subtracting one from the day part until it is a valid date. This should really be fixed on the entry side though.
Try this:
var date = "20140231";
DateTime scope;
bool dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
while (!dateValid)
{
string yearMonth = date.Substring(0, 4);
int day = Convert.ToInt32(date.Substring(6, 2));
if (day > 1)
{
day--;
}
else
{
break;
}
date = yearMonth + day.ToString().PadLeft(2, '0');
dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
}
How to convert timestamp to datetime in c#, but only want the date not included the hour minutes and seconds?
by using this code i have full datetime:
double timestamp = Convert.ToDouble(timestamp.Text);
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp);
lblDate.Text = dateTime.ToString(); //Latest Timestamp
just because I just want the date only I remove
dateTime = dateTime.AddSeconds(timestamp);
and change this line
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
to:
System.DateTime dateTime = new System.DateTime(1970, 1, 1);
then the result that i got for date is not correct
If you want just date part you can use DateTime.ToShortDateString() or DateTime.ToLongDateString() method:
lblDate.Text = dateTime.ToShortDateString();
You can also use Standard or Custom DateTime format string. E.g.
lblDate.Text = dateTime.ToString("d"); // same as above
lblDate.Text = dateTime.ToString("D"); // same as ToLongDateString
lblDate.Text = dateTime.ToString("dd-MMM-yyyy");
Try This.
DateTime date = new DateTime(Convert.ToInt64(timestamp.Text));
After that convert your datetime in date format
if your datetime containing variable is dateTime
so convert its values as
string StrDate=dateTime.ToString("dd/MM/yyyy");
finaly u will get only date in "dd/MM/yyyy" format
How to get the start date and end date of month in different variable. I have tried this and I get the start date but unable to find the end date
DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");
This logic fails when month end date is 31 and 28 or 29. Your Help are surely appretiated.
You can calculate endDate like this:
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
To get First Date
public DateTime FirstDayOfMonth(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
To get Last Date
public DateTime LastDayOfMonth(DateTime dateTime)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
.AddMonths(1).AddDays(-1);
You already had the start date :
DateTime monthStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
There's a method to get the number of days in a month (and Looking at the IL code, it seems that this way is more efficient than the other answers, though unless you're going to do it a billion time, I doubt there will be any difference) :
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime monthEndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, daysInMonth);
For first date:
DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
For last date:
DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));