Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a problem with the DateTime.ParseExtract. The error is String was not recognized as a valid DateTime.
trialDate in the sqlserver 2012 is define as folllow type=date and it is display in this format
MM/dd/yyyy. I guess it is the default for sqlserver. I don't know how and where to change it to
dd/MM/yyyy or dd-MM-yyyy.
When I display the date in the listview I did this (put a mask):
'
Here is the C# code-behind
string sTrialDate = "";
foreach (ListViewDataItem item in ListView1.Items)
{
CheckBox MyCheckBox = (CheckBox)item.FindControl("MyCheckBox");
if (MyCheckBox.Checked)
{
Label myTrialDate = (Label)item.FindControl("trialDatelbl");
sTrialDate = myTrialDate.Text;
}
}
The problem is the DATETIME.ParseExtract, here is what I tried so far:
DateTime dt = DateTime.ParseExact("sTrialDate", "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact("sTrialDate", "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact("sTrialDate", "dd/MM/yyyy ",
System.Globalization.CultureInfo.InvariantCulture); <=== not working
DateTime dt = DateTime.ParseExact(Request.QueryString["sTrialDate"], "dd/MM/yyyy", null);
<==not working
You seem to be misusing ParseExact. You are doing:
ParseExact("sDateRdv", "MM/dd/yyyy")
Which is trying to parse the string sDateRdv as a date and obviously failing. If sDateRdv is a variable containing your date then you should use:
ParseExact(sDateRdv, "MM/dd/yyyy")
In your last example you should check what the value of Request.QueryString["sTrialDate"] is. Is it a valid date string?
Lastly you talk about a date in sqlserver and how to change its representation. The answer is you don't really. A date is a date and is independant of how it is displayed. When you look at it it will be converted to a human readable string (because that's all we read) but that doesn't represent how it is stored. You would treat it as a date the whole way (so get it from the database into your C# code as a DateTime) and you only ever convert it to a string for display or maybe some kinds of serialisation (eg XML).
Apart from the fact that you should not pass your variable in quotes:
DateTime dt = DateTime.ParseExact(sDateRdv, "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
DateTime dt = DateTime.ParseExact(sDateRdv, "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
DateTime dt = DateTime.ParseExact(sDateRdv, "dd/MM/yyyy ",
System.Globalization.CultureInfo.InvariantCulture);
You could also try this:
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = "dd-MM-yyyy";
DateTime objDate = Convert.ToDateTime(someStringWithDate, dtfi)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
string aa = txtsd.Text;
string bb = txtendd.Text;
DateTime dt1 = Convert.ToDateTime(aa);
DateTime dt2 = Convert.ToDateTime(bb);
//DateTime dt2 = Convert.ToDateTime(bb,System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
//DateTime dt2 = DateTime.ParseExact(txtendd.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
TimeSpan ts = (dt1 - dt2);
The above code which I have tried in many ways is to calculate the two dates but I keep on getting two errors can any help me in sorting out the two errors.
Errors-
1] The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
2] string was not recognized as a valid datetime.
The methods you are looking for are:
DateTime.Parse Method
DateTime.TryParse Method
DateTime.ParseExact Method
DateTime.TryParseExact Method
As this is from user input I would suggest the Try variants.
An example if you know the format and it's not the default for your culture:
var stringDateTime = "15/01/2013";
DateTime date;
if(DateTime.TryParseExact(stringDateTime, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
// awesome lets do something
}
string aa = "15/01/2000";
string bb = "20/01/2000";
DateTime dt1 = DateTime.ParseExact(aa, "dd/MM/yyyy", null);
DateTime dt2 = DateTime.ParseExact(bb, "dd/MM/yyyy", null);
TimeSpan ts = (dt1 - dt2);
Console.WriteLine($"Difference: {ts.Days}");
Console.ReadKey();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wanna convert MM/YY formatted string date to mm-yy DateTime. And set as the value for rad masked edit box. But it returns to me
"String was not recognized as a valid DateTime."
I tried with
DateTime dt = DateTime.ParseExact("11/17", "MMyy", CultureInfo.InvariantCulture);
for eg, I want to convert 03/16 and set value of radmasked edit box masked as MMyy as 03-16
Why do you expect this to work at all?
DateTime dt = DateTime.ParseExact("11/17", "MMyy", CultureInfo.InvariantCulture);
You get a string 11/17 and try to parse it with a format that doesn't contain any delimiters.
This works:
DateTime dt = DateTime.ParseExact("11/17", "MM/yy", CultureInfo.InvariantCulture);
If you want to convert it to a string with this format: MMyy:
string result = dt.ToString("MMyy", CultureInfo.InvariantCulture);
Since it's not clear, if you want this instead: MM-yy
string result = dt.ToString("MM-yy", CultureInfo.InvariantCulture);
It's working for 11/17,12/17. but not in the case of 3/12 etc i.e when
a month is a single digit.
You haven't mentioned that it's possible that the month has a single digit, however:
DateTime dt = DateTime.ParseExact("3/17", "M/yy", CultureInfo.InvariantCulture);
This will work for you.
DateTime dt = DateTime.ParseExact("11/17", "MM/yy", CultureInfo.InvariantCulture);
Then you can convert it to your desired format
string formattedDate = dt.ToString("MM-yy");
In case you want to change delimeter '/' into '-' only, i.e. if you have no need in DateTime temporary value, you can just Replace:
string source = "11/17";
// 11-17: changing '/' to '-'
string result = source.Replace('/', '-');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have string time. lets say '06:35 PM'. I want to convert the string to DateTime.
The date must be current time(the day as they input the time).
I did
string times = endTime;
DateTime dt;
(DateTime.TryParseExact(times, "YYYY-MM-dd HH:mm tt", CultureInfo.InvariantCulture,DateTimeStyles.None, out dt))
But it didn't works. it make a null value. because when I try to put the result on label, the label did not show anything. and also I have been try
var date = DateTime.Parse("06:45 AM");
Console.WriteLine(date);
But it didn't works too and it makes an error.
ERROR System.Data.SqlClient.SqlException (0x80131904): The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. "
How do I convert it ?
I think you are passing the string "06:45 AM" to the database and this is not a valid entry since the DB does not know how to store it, hence the exception.
Looking a bit further, in your first example, YYYY should be lowercase yyyy. YYYY will not parse properly correctly in to a date format.
Considering the second example you have two options:
1) When writing to the database, make sure you pass a valid date, e.g. below. You may need to try a few formats to match up to what your DB expects but it will need a full date and time.
var date = string.Format("{0:yyyy-MM-dd HH:mm tt}", DateTime.Parse("06:45 AM"));
Console.WriteLine(date);
2) Use a full DateTime approach using the correct SQL parameter type. This looks like a good explanation covering a few gotchas. Using DateTime in a SqlParameter for Stored Procedure, format error
If day doesn't matter, you can use following
DateTime date;
if (DateTime.TryParseExact("06:45 AM", new[] {"h:mm tt"}, null, DateTimeStyles.None, out date))
{
Console.WriteLine(date);
Console.WriteLine(date.TimeOfDay);
}
I think the below code will solve your issue;
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(06, 45, 0);
dt = dt.Date + ts;
To convert string to DateTime format, use the below code;
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
i have string format of date looks like "04/16/2014 19:10", i want to convert it to DateTime.
i tried, below codes, but it didn't work. i got error like "String was not recognized as a valid DateTime."
How to convert to datetime
DateTime dt1 = DateTime.Parse(DateTimeString);
DateTime dt = System.Convert.ToDateTime(DateTimeString);
The problem is Parse, as you are using it, will take into account the current culture of the machine which means (depending on where you are) that date could be interpreted differently.
Whenever you are parsing specific dates you should use ParseExact or TryParseExact, that way you leave no room for ambiguity on how the date should be interpreted (regardless of culture)
DateTime dt;
if (DateTime.TryParseExact("04/16/2014 19:10", "MM/dd/yyyy hh:mm",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
// date was parsed correctly, use `dt`
}
You may want to use ParseExact and specify the format yourself:
DateTime d = DateTime.ParseExact("04/16/2014 19:10", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying not to format ToString(). I need the date itself in the format.i cannot do to string
scenario:
string str = "02/11/2006";
DateTime dt = new DateTime();
dt = Convert.ToDateTime(str);
now dt is in the format mm/dd/yyyy, but i need in the format yyyy-mm-dd
hope i add some sense..huh!
Not sure what you mean. If you have the date in a DateTime variable it is an universal representation of the date. If you wish to convert it back to string you can use the following:
string str = "02/11/2006";
DateTime dt = new DateTime();
dt = Convert.ToDateTime(str);
var date = dt.ToString("yyyy-MM-dd");
You are confusing the DateTime Structure with its string presentation.
if you want to convert a string with a custom DateTime format you have to use DateTime.ParseExact method
dt = DateTime.ParseExact(str ,"mm/dd/yyyy",CultureInfo.InvariantCulture);
if you want a particular string presentation of a DaTime you have to use a format string
dt.ToString("yyyy-mm-dd")
A DateTime has no format (it's only data). Only String representation have one. Add this to your code :
String newFormatedDate = dt.ToString("yyyy-MM-dd");
I think you just need to know about the ToString overloads:
DateTime newDate = DateTime.ParseExact("15/01/2001", "dd/MM/yyyy", null);
Console.WriteLine(newDate.ToString("yyyy-MM-dd"));