Is there a setting in visual studio for the Convert.ToDateTime?
-
Hi, Why is it that my
Convert.ToDateTime
isn't working any more... Usually when i saidDateTime Test = Convert.ToDateTime(System.DateTime.Now);
the value of Test was "2006/09/11 14:00:00 PM" now it is "11/9/2006" not at all what i want... Is there a setting to change this? The regional settings are the same as when theConvert.ToDateTime
work right..."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Hi, Why is it that my
Convert.ToDateTime
isn't working any more... Usually when i saidDateTime Test = Convert.ToDateTime(System.DateTime.Now);
the value of Test was "2006/09/11 14:00:00 PM" now it is "11/9/2006" not at all what i want... Is there a setting to change this? The regional settings are the same as when theConvert.ToDateTime
work right..."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Hi, Why is it that my
Convert.ToDateTime
isn't working any more... Usually when i saidDateTime Test = Convert.ToDateTime(System.DateTime.Now);
the value of Test was "2006/09/11 14:00:00 PM" now it is "11/9/2006" not at all what i want... Is there a setting to change this? The regional settings are the same as when theConvert.ToDateTime
work right..."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
I really don't understand what you are trying to do because
System.DateTime.Now
already returns a DateTime object. So theConvert.ToDateTime()
method call is redundant - it does nothing.NarutoFan#1 wrote:
the value of Test was "2006/09/11 14:00:00 PM" now it is "11/9/2006" not at all what i want
No, the value of Test is a DateTime object (which has no culture specific representation)
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
I might be wrong but wouldn't this:
Test.ToString("MM/dd/yyyy");
bring back the value "11/09/2006"... I'm looking for the value of "2006/09/11 14:00:00 PM" Usually theConvert.ToDateTime
brought back this value that i'm looking for without a problem... on my friends PC thisConvert.ToDateTime(System.DateTime.Now);
brings back the value "2006/09/11 14:00:00 PM" but not on mine... as it brings back the wrong value format..."11/09/2006" What could the reason be for this? Thank you in advance... Regards Nico"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
I really don't understand what you are trying to do because
System.DateTime.Now
already returns a DateTime object. So theConvert.ToDateTime()
method call is redundant - it does nothing.NarutoFan#1 wrote:
the value of Test was "2006/09/11 14:00:00 PM" now it is "11/9/2006" not at all what i want
No, the value of Test is a DateTime object (which has no culture specific representation)
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
I just used the
System.DateTime.Now
as and example. That could be a string "2006/12/12 14:00:00 PM", The thing is ... usually when i used the Convert.ToDateTime("WHATEVER") the correct value (being "2006/09/11 12:00:00 PM" so the format was "yyyy/MM/dd hh:mm:ss") but now it brings back a value of "11/09/2006" which is "dd/MM/yyyy". How could this be? I change nothing that i know of.... Please help."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
I just used the
System.DateTime.Now
as and example. That could be a string "2006/12/12 14:00:00 PM", The thing is ... usually when i used the Convert.ToDateTime("WHATEVER") the correct value (being "2006/09/11 12:00:00 PM" so the format was "yyyy/MM/dd hh:mm:ss") but now it brings back a value of "11/09/2006" which is "dd/MM/yyyy". How could this be? I change nothing that i know of.... Please help."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
I suggest that instead of using Convert, use DateTime.Parse. See Example from MSDN below: using System; using System.Globalization; namespace Parse { class Class1 { public static void Main(string[] args) { // Assume the current culture is en-US. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string myDateTimeValue = "2/16/1992 12:15:12"; DateTime myDateTime = DateTime.Parse(myDateTimeValue); Console.WriteLine("1) myDateTime = {0}", myDateTime); // Reverse month and day to conform to a different culture. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. IFormatProvider culture = new CultureInfo("fr-FR", true); string myDateTimeFrenchValue = " 16/02/1992 12:15:12"; DateTime myDateTimeFrench = DateTime.Parse(myDateTimeFrenchValue, culture, DateTimeStyles.NoCurrentDateDefault); Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench); // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string[] expectedFormats = {"G", "g", "f" ,"F"}; myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces); Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench); } } } /* This example yields the following results: 1) myDateTime = 2/16/1992 12:15:12 PM 2) myDateTimeFrench = 2/16/1992 12:15:12 PM 3) myDateTimeFrench = 2/16/1992 12:15:12 PM */ if (ToErr == Human.Nature) { Forgive = Divine; }
-
I might be wrong but wouldn't this:
Test.ToString("MM/dd/yyyy");
bring back the value "11/09/2006"... I'm looking for the value of "2006/09/11 14:00:00 PM" Usually theConvert.ToDateTime
brought back this value that i'm looking for without a problem... on my friends PC thisConvert.ToDateTime(System.DateTime.Now);
brings back the value "2006/09/11 14:00:00 PM" but not on mine... as it brings back the wrong value format..."11/09/2006" What could the reason be for this? Thank you in advance... Regards Nico"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
I suggest that instead of using Convert, use DateTime.Parse. See Example from MSDN below: using System; using System.Globalization; namespace Parse { class Class1 { public static void Main(string[] args) { // Assume the current culture is en-US. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string myDateTimeValue = "2/16/1992 12:15:12"; DateTime myDateTime = DateTime.Parse(myDateTimeValue); Console.WriteLine("1) myDateTime = {0}", myDateTime); // Reverse month and day to conform to a different culture. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. IFormatProvider culture = new CultureInfo("fr-FR", true); string myDateTimeFrenchValue = " 16/02/1992 12:15:12"; DateTime myDateTimeFrench = DateTime.Parse(myDateTimeFrenchValue, culture, DateTimeStyles.NoCurrentDateDefault); Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench); // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string[] expectedFormats = {"G", "g", "f" ,"F"}; myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces); Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench); } } } /* This example yields the following results: 1) myDateTime = 2/16/1992 12:15:12 PM 2) myDateTimeFrench = 2/16/1992 12:15:12 PM 3) myDateTimeFrench = 2/16/1992 12:15:12 PM */ if (ToErr == Human.Nature) { Forgive = Divine; }
Okay thanks, But why would
DateTime TodayTest = DateTime.Now;
produce the value "2006/09/11 12:00:00 PM" on my friend's computer but on mine it shows "11/09/2006". What could be the symptoms?"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
I just used the
System.DateTime.Now
as and example. That could be a string "2006/12/12 14:00:00 PM", The thing is ... usually when i used the Convert.ToDateTime("WHATEVER") the correct value (being "2006/09/11 12:00:00 PM" so the format was "yyyy/MM/dd hh:mm:ss") but now it brings back a value of "11/09/2006" which is "dd/MM/yyyy". How could this be? I change nothing that i know of.... Please help."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
Naruto You've asked the same question several times. I think you need to provide a little bit more information before we can help you. Here's a few things to answer/think about: What type of application is this? Is it WinForms or ASP.NET? How are you attempting to print out the date? Have you printed out the DateTime.ToLongTimeString() to see if the time is there. BTW - you can't have a value of 14:00:00 PM;) As I stated in an earlier answer to you, I suspect that your problem is the CurrentCulture. If you are using ASP.NET, then it is entirely possible that the problem is because the culture of the user that ASP.NET runs under is different to yours. To solve this, you can use a "clever" trick to sort this out. In your Page_Load method, call the following routine:
public static void SetCulture(HttpRequest req) { if (req != null && req.UserLanguages != null && req.UserLanguages.Length > 0) { SetCulture((string)req.UserLanguages[0]); } } public static void SetCulture(string culture) { CultureInfo ci = new CultureInfo(culture); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }
If you call the first method, the user will get data presented to them in the culture that they have set up on their machine. So, somebody from France would probably get information back for the culture fr-FR. Now, calling the second method means that you can specify the culture explicitly, and the user will get information back in that culture, so the same French user would get back information with GB settings if you calledSetCulture("en-GB");
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.
-
Naruto You've asked the same question several times. I think you need to provide a little bit more information before we can help you. Here's a few things to answer/think about: What type of application is this? Is it WinForms or ASP.NET? How are you attempting to print out the date? Have you printed out the DateTime.ToLongTimeString() to see if the time is there. BTW - you can't have a value of 14:00:00 PM;) As I stated in an earlier answer to you, I suspect that your problem is the CurrentCulture. If you are using ASP.NET, then it is entirely possible that the problem is because the culture of the user that ASP.NET runs under is different to yours. To solve this, you can use a "clever" trick to sort this out. In your Page_Load method, call the following routine:
public static void SetCulture(HttpRequest req) { if (req != null && req.UserLanguages != null && req.UserLanguages.Length > 0) { SetCulture((string)req.UserLanguages[0]); } } public static void SetCulture(string culture) { CultureInfo ci = new CultureInfo(culture); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }
If you call the first method, the user will get data presented to them in the culture that they have set up on their machine. So, somebody from France would probably get information back for the culture fr-FR. Now, calling the second method means that you can specify the culture explicitly, and the user will get information back in that culture, so the same French user would get back information with GB settings if you calledSetCulture("en-GB");
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.
No it's winforms. Thank you...i tried your previous example that you gave me...
// Force the culture to English (GB). CultureInfo culture = new CultureInfo("en-GB"); DateTime dateTime = Convert.ToDateTime(_appDate+" "+startTime, culture); DateTime dateTime2 = Convert.ToDateTime(_appDate+" "+endTime, culture);
but this brought back the exact same date "11/09/2006" as when i tried just tostring _fromDateTime = _appDate+" "+startTime; string _endDateTime = _appDate+" "+endTime; FromTime = Convert.ToDateTime(_fromDateTime); ToTime = Convert.ToDateTime(_endDateTime);
but on my friends pc it brings back the value "2006/09/11 02:00:00 PM" Sorry, in know i'm not explaing enough what my problem is but i'm not even sure what is wrong... its just when i try to compare to dates like in this example:if( _today>=vch.FromDate && _today <=vch.ToDate)
_today
has the value of todays date... on my computer it is "11/09/2006" the value ofvch.FromDate
is "11/09/2006" and the value ofvch.ToDate
is "11/09/2006"... This if statement should return true... Am i right. On my computer it returns false... ???? :confused: that is just one example... another one was yesterday's problem, but some how that sorted itself out... when the selected time "2006/09/11 02:00:00 PM" was writen into the database the the time would be inserted as AM but without the AM if that makes sense (like so: "2006/09/11 02:00:00.000)... Now it writes the time in the db correctly... "2006/09/11 14:00:00.000" Weird but true..."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
No it's winforms. Thank you...i tried your previous example that you gave me...
// Force the culture to English (GB). CultureInfo culture = new CultureInfo("en-GB"); DateTime dateTime = Convert.ToDateTime(_appDate+" "+startTime, culture); DateTime dateTime2 = Convert.ToDateTime(_appDate+" "+endTime, culture);
but this brought back the exact same date "11/09/2006" as when i tried just tostring _fromDateTime = _appDate+" "+startTime; string _endDateTime = _appDate+" "+endTime; FromTime = Convert.ToDateTime(_fromDateTime); ToTime = Convert.ToDateTime(_endDateTime);
but on my friends pc it brings back the value "2006/09/11 02:00:00 PM" Sorry, in know i'm not explaing enough what my problem is but i'm not even sure what is wrong... its just when i try to compare to dates like in this example:if( _today>=vch.FromDate && _today <=vch.ToDate)
_today
has the value of todays date... on my computer it is "11/09/2006" the value ofvch.FromDate
is "11/09/2006" and the value ofvch.ToDate
is "11/09/2006"... This if statement should return true... Am i right. On my computer it returns false... ???? :confused: that is just one example... another one was yesterday's problem, but some how that sorted itself out... when the selected time "2006/09/11 02:00:00 PM" was writen into the database the the time would be inserted as AM but without the AM if that makes sense (like so: "2006/09/11 02:00:00.000)... Now it writes the time in the db correctly... "2006/09/11 14:00:00.000" Weird but true..."Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
What is vch? Is it a calendar control or something? Could this be the source of your troubles?
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.
-
What is vch? Is it a calendar control or something? Could this be the source of your troubles?
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.
the vch part is a class parameter. it is property in a class.
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
the vch part is a class parameter. it is property in a class.
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
Naruto Try the following wrapper class and test:
using System; using System.Collections.Generic; using System.Text; namespace DateWrapper { public class WrappedDate { private DateTime? _dt = null; /// /// Initialize a new instance of . /// public WrappedDate() { } /// /// Initialize a new instance of . /// /// The date/time to wrap. public WrappedDate(DateTime dt) { _dt = dt; } /// /// Initialize a new instance of . /// /// The date part without a time. public WrappedDate(string dateOnly) : this(dateOnly, null) { } /// /// Initialize a new instance of . /// /// The date without the time /// The time portion. public WrappedDate(string date, string time) { if (string.IsNullOrEmpty(time)) time = "00:00:00"; if (!time.StartsWith(" ")) time = " " + time; while (time.IndexOf(" ") > -1) { time = time.Replace(" ", " "); } try { _dt = Convert.ToDateTime(date + time); } catch (Exception ex) { _dt = null; } } public DateTime? Date { get { return _dt; } } } class Program { static void Main(string[] args) { WrappedDate start = new WrappedDate(DateTime.Now); WrappedDate dt = new WrappedDate("20/12/2006", "10:00:00"); WrappedDate finish = new WrappedDate(DateTime.Now.AddHours(1)); WrappedDate compare = new WrappedDate(DateTime.Now.AddMinutes(30)); Console.WriteLin
-
What is vch? Is it a calendar control or something? Could this be the source of your troubles?
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.
Hi Pete, i have great news... The problem is only on my pc. As soon as i build and install onto another pc it works fine... The variable shows me the wrong values in the debugger but somehow it contains the correct values and writes these correct values into the database. The problem still eludes me, but for now it at least works on the client's computers. Thank you for taking the time to help out... :) Regards Nico
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Okay thanks, But why would
DateTime TodayTest = DateTime.Now;
produce the value "2006/09/11 12:00:00 PM" on my friend's computer but on mine it shows "11/09/2006". What could be the symptoms?"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
NarutoFan#1 wrote:
But why would DateTime TodayTest = DateTime.Now; produce the value "2006/09/11 12:00:00 PM" on my friend's computer but on mine it shows "11/09/2006". What could be the symptoms?
DateTime
, as I've already said, has no cultural bias. It has no display format. There are a few methods onDateTime
that convert it into a culturally biased string. Most of these methods take the culture of the OS (i.e. what is in the control panel for how to display date and time information). TheToString()
method onDateTime
can be supplied with any format you like if you want to get specific. e.g.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Hi Pete, i have great news... The problem is only on my pc. As soon as i build and install onto another pc it works fine... The variable shows me the wrong values in the debugger but somehow it contains the correct values and writes these correct values into the database. The problem still eludes me, but for now it at least works on the client's computers. Thank you for taking the time to help out... :) Regards Nico
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
That's great news.:) I'll have a think about it some more, but it sounds as though the problem might have been with the debugger. As long as the info gets written properly, then that's all right.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.