C# DatePicker convert to DateTime and calculate the Age [duplicate] - c#

This question already has answers here:
A property or indexer may not be passed as an out or ref parameter
(9 answers)
Closed 4 years ago.
I have a DatePicker(CPTestDP) in our WPF window, and the date picked is saved into SQL datebase as text. I would like to use calculate the Age(how many days) based on today's date by below code. But it turns error at CFTestDP.SelectedDate.
The error message says:
A property or indexer may not be passed as an out or ref parameter.
Could anyone help please? Much appreciate.
DateTime thisDay = DateTime.Today;
DateTime startDay = DateTime.TryParse(CFTestDP.SelectedDate, out CFTestDP.SelectedDate);
TimeSpan dateAge = thisDay - startDay;
txtAge.Text = string.Format("{dd}", dateAge);

As I have seen in the documentation you should pass as second parameter in the TryParse a reference to a variable of type DateTime and it will return a boolean that tells you if the parse was ok or not. So your code should be like this:
DateTime thisDay = DateTime.Today;
DateTime startDay;
bool result = DateTime.TryParse(CFTestDP.SelectedDate, out startDay);//CFTestDP.SelectedDate should be string
if(result)
{
TimeSpan dateAge = thisDay - startDay;
txtAge.Text = string.Format("{dd}", dateAge);
}
else
{
//Unable to parse
}
Source: https://msdn.microsoft.com/es-es/library/ch92fbc1(v=vs.110).aspx

Related

How to convert date "2018-12-13T07:33:35.893Z" to 13/12/2018 [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");

Get original DateTime offset [duplicate]

This question already has answers here:
Converting string to DateTime with offset
(5 answers)
Closed 4 years ago.
I have following code
string dateString = "2018-04-20T12:22:32.8526432-05:30";
var objDate = DateTime.Parse(dateString);
string newDateString = objDate.ToString(); //"4/20/2018 1:52:32 PM"
Once string is parsed to DateTime how do I get original DateTime offset i.e. -5:30 from objDate? I tried following code but it gives local offset i.e. -4:00 but not -5:30.
var offset = TimeZone.CurrentTimeZone.GetUtcOffset(objDate);
Please note that I want to get offset from DateTime object (objDate) and not from string variable dateString.
As mentioned by #Evk the DateTime type does not store the offset. Instead as suggested by #maccettura
string dateString = "2018-04-20T12:22:32.8526432-05:30";
var offset = DateTimeOffset.Parse(dateString);
// returns -05:30:00
Console.WriteLine(offset.Offset);

c# creating datetime object without time [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 6 years ago.
I am trying to load some dates from a database, but i dont want to create a datetime object with time, i only want the date. I have tried with
var date = value.Date; it doesn't work. I have seen a lot of methods that basically format the date to show it as a string and i don't want to do that, because than i have to load all the data into the grid manually. I hope someone can help me. Here is the code
DateTime startDate = (DateTime)row["Course_StartDate"];
DateTime endDate = (DateTime)row["Course_EndDate"];
Do you need to use any of the functionality of DateTime, such as comparing dates or adding/subtracting days from a date? If not, you can probably just make your variables strings instead of DateTimes:
string startDate = ((DateTime)row["Course_StartDate"]).ToShortDateString();
string endDate = ((DateTime)row["Course_EndDate"]).ToShortDateString();
Edit: Since you do need to compare times, you could use a second property that returns the equivalent value as you need it:
DateTime startDate = new DateTime();
DateTime endDate = new DateTime();
string startDateString
{
get
{
return startDate.ToShortDateString();
}
}
string endDateString
{
get
{
return endDate.ToShortDateString();
}
}
private void LoadMethod()
{
startDate = (DateTime)row["Course_StartDate"];
endDate = (DateTime)row["Course_EndDate"];
}
Then you can do your math with the DateTime objects, and use the string objects in your table.
If you are talking about DataGridView, change the Column Format (DefaultCellStyle > Format) to MM/dd/yyyy

Convert string with date & UTC offset to DateTime [duplicate]

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".
I tried the following but it gives invalid DateTime exception.
DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));
Is there a way to convert the String exactly to Datetime with UTC offset value?
To preserve your Offset, use the DateTimeOffset.ParseExact method:
string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
I would suggest to try out one of ParseExact methods of DateTime class

String date to DayOfWeek [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
This code is a simplified version of what I'm trying to do:
this.dateValue = "8/12/2005";
return DateTime.dateValue.DayOfWeek.ToString();
I want to use my dateValue in stead of 'NOW'
Use DateTime.TryParseExact
this.dateValue = "8/12/2005";
DateTime dt;
// assuming the expected date is the 8th of Dec 2005, otherwise use m/d/yyyy
if (DateTime.TryParseExact(this.dateValue, "d/m/yyyy", CulterInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
return dt.DayOfWeek.ToString();
}
else
{
return null;
}
The DateTime.Now property returns a DateTime, DayOfWeek is a property of DateTime so you can simply do
return DateTime.Now.DayOfWeek.ToString();
To get the text version of the current Day of the Week
If you want to reverse this you can parse a new DateTime from the string.
return DateTime.Parse("8/12/2005").DayOfWeek.ToString();
Watch our for strings which can't be parsed. You may want to look into the TryParse method.

Categories