1

I have a list, where one column is of type datetime and another a numeric value.

The records are being entered correctly, in the form:

 ID  Col01       Col02
 01  05/02/2013  6
 02  05/03/2013  9

Code I have used (snippet):

_item["_x0043_ol01"] = DateTime.Parse(mtbRecordDate.Text).AddDays(1).ToString("MM/dd/yyyy");
_item["_x0043_ol02"] = mtbNum.Text;

I am plotting these records in a graph using MSChart control, with dates on the horizontal axis and numeric values on the vertical axis. The graph is fine, and draws correctly.

But the date values it displays are wrong. It sometimes displays the correct date, at others it adds one to the date and then displays it. eg. record 02 plotted as 05/04/2013.

I've tried changing the format, and have crosschecked the values which are being passed to the graph. And they are correct.

Also, if I go back and delete the date, then enter it again, it plots correctly. Any idea why this happening? Any help is much appreciated.

PS: I'm in a timezone different from the server. Could that be the reason? The date is simply being stored as only the date, not the time or timezone.

2
  • You say you're in a different timezone to the server, are you also using a different Local? ie US/non-US?
    – Neil White
    Commented May 3, 2013 at 15:31
  • Local is non-US, too
    – xcentaur
    Commented May 5, 2013 at 14:40

1 Answer 1

0

try

DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

more importantly:

The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter. It also requires that the and elements of the string representation of a date and time appear in the order specified by format, and that s have no white space other than that permitted by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00). If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date.

If s does not represent a time in a particular time zone and the parse operation succeeds, the Kind property of the returned DateTime value is DateTimeKind.Unspecified. If s does represent the time in a particular time zone and format allows time zone information to be present (for example, if format is equal to the "o", "r", or "u" standard format specifiers, or if it contains the "z", "zz", or "zzz" custom format specifiers), the Kind property of the returned DateTime value is DateTimeKind.Local.

The format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of s. For details about valid formatting codes, see Standard Date and Time Format Strings or Custom Date and Time Format Strings.

so if you know its correct and it knows its correct but gives a false value because of .net than this will fix the issue by forcing the check and layout

http://msdn.microsoft.com/en-GB/library/w2sa9yss.aspx

so your code will look like this:

_item["_x0043_ol01"] = DateTime.ParseExact(mtbRecordDate.Text, "dd-MMM-yyyy", null)AddDays(1).ToString("MM/dd/yyyy");
_item["_x0043_ol02"] = mtbNum.Text;
3
  • mtbRecordDate is actually a masked text box. sorry, should have included that in the question itself. and in that, the specific format i've put is MM/dd/yyyy. the addDays(1) was a misguided attempt to fix it. i've removed it and the code is now: _item["_x0043_ol01"] = DateTime.ParseExact(mtbRecordDate.Text, "MM/dd/yyyy", null).ToString("MM/dd/yyyy");
    – xcentaur
    Commented May 5, 2013 at 14:49
  • just trying it now, will post back
    – xcentaur
    Commented May 5, 2013 at 14:50
  • no probs happy to help ;)
    – Ali Jafer
    Commented May 7, 2013 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.