Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting expiry date from XML file and convert to datetime. it's work on debug mode. but after deploy the source code it's return error.

public LabelInfo ParseLabelsingalData(XmlNode Llist)
{
LabelInfo labelinfor = new LabelInfo();

labelinfor.ID = Llist.Attributes["Name"].Value;
labelinfor.Name = Llist.Attributes["Name"].Value;

labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes["ExpiryDate"].Value);
}

I am getting date from xml like "31/01/2018"

return error is - string was not recognized as a valide datetime

What I have tried:

I have tried to convert date time labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes["ExpiryDate"].Value);
Posted
Updated 18-Oct-18 1:34am
Comments
Slacker007 18-Oct-18 11:36am    
Solution 1 (and 2) are the answers to you problem. Do note that when using TryParse, you should use it in an "If" statement, which means you will need to write additional code to handle the scenario of a bad date - what do you do when the date cannot be parsed.

The problem is almost certainly that the DateTime format used on your development machine is not the same as that on the production machine - and Convert.ToDateTime uses the current machine setting as the format for the conversion process.

I'd strongly recommend that you forget all the Convert functions, and look at TryParse or TryParseExact in future.

In this case, I'd check your XML data and use DateTime.TryParseExact to specify exactly the format the file will contain, so you aren;t reliant in any way on machine settings.
 
Share this answer
 
Comments
Eranga Dayarathne 18-Oct-18 22:34pm    
So in this case it's work. thanks. but when i equal this format to again datetime variable it show MM/dd/yyyy format.

this is the wahy i did,

string format = "dd/MM/yyyy";

if (DateTime.TryParseExact(dtime, format,
System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out Temdate))
{
labelinfor.MinimumExpiryDate = Temdate;
}

SysLog.WriteLog(MSLogLevel.INFORMATIONAL, "Trace ExpiryDate - " + labelinfor.ExpiryDate);


"labelinfor.MinimumExpiryDate is a datetime varible"

private DateTime expiryDate = DateTime.Now;

public DateTime ExpiryDate
{
get
{
return expiryDate;
}
set
{
expiryDate = value;
}
}


why it's again change when i equal to datetime ?
OriginalGriff 19-Oct-18 2:25am    
Because a DateTime value has no format - it's stored as a number of ticks since a fixed point in time, and doesn't become a year, month, and day until it is converted back into a string for presentation to the user.
Whereupon if you don't specify a format it again uses the current machine setting to provide a date format it thinks the user should be comfortable with!
Do not use Convert for such issues, use DateTime.TryParse[^]. This allows more control over the format of the date. Remember that date representations vary according to culture; e.g. in US date format, 31/01/2018 is not valid as 31 is the month field.
 
Share this answer
 
Comments
Eranga Dayarathne 18-Oct-18 22:34pm    
So in this case it's work. thanks. but when i equal this format to again datetime variable it show MM/dd/yyyy format.

this is the wahy i did,

string format = "dd/MM/yyyy";

if (DateTime.TryParseExact(dtime, format,
System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out Temdate))
{
labelinfor.MinimumExpiryDate = Temdate;
}

SysLog.WriteLog(MSLogLevel.INFORMATIONAL, "Trace ExpiryDate - " + labelinfor.ExpiryDate);


"labelinfor.MinimumExpiryDate is a datetime varible"

private DateTime expiryDate = DateTime.Now;

public DateTime ExpiryDate
{
get
{
return expiryDate;
}
set
{
expiryDate = value;
}
}


why it's again change when i equal to datetime ?
Richard MacCutchan 19-Oct-18 3:23am    
It doesn't change. A DateTime is just a number, but how it displays the content will depend on the environment and the current culture.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900