I am using LINQ and have a few properties thats DateTime? type.
If i now want to add the value from a textbox i cant seem to get this to work.
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ScoringLastUpgrade", DbType="Date")]
public System.Nullable<System.DateTime> ScoringLastUpgrade
The textbox i use i have made sure with javascript that the format will be '2011-06-17'
But now when i try to do this:
myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text).ToShortDateString();
I get this error: "Cannot convert type string to DateTime?"
How to do this?
The .ToShortDateString() call is converting it into a string. You should remove that call.
Also, you say you've made sure of the format with javascript. What if the user doesn't have javascript, you should do server-side checks too. Also, since it's in a given format, you can use DateTime.ParseExact or DateTime.TryParseExact (so an invalid format doesn't throw an exception) since its more efficient. (The format string would be "yyyy-MM-dd") i believe.
Don't convert it to string using 'ToShortDateTimeString', just set the result of Convert.ToDateTime:
myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text);
Assuming you've done sufficient validation on txtScoringUpgradeDate.Text?
My preference when dealing with these type conversions is to use the TryParse method, e.g.:
DateTime date;
if (DateTime.TryParse(txtScoringUpgradeDate.Text, out date))
myObject.ScoringLastUpgrade = date;
The Convert.ToDateTime, much like the explicit DateTime.Parse will throw an InvalidCastException when an excepional value occurs. It's better to make your code fault tollerant then needlesly catch an exception.
UPDATE: based on your last comment:
You shouldn't return DateTime.MinValue in this case, as MinValue is less than the supported min value of a datetime column. the CLR DateTime supports a date range down to 0000-01-01, whereas the SQL datetime (as well as the comparative CLR SqlDateTime type) supports a minimum value of 1753-01-01. As it as a nullable DateTime, you should set it to null:
public static DateTime? ToNullableDateTime(this string date)
{
DateTime dateTime;
return (DateTime.TryParse(date, out dateTime))
? (DateTime?)dateTime
: null;
}
The problem is that you have put ToShortDateString() at the end there, effectively converting the DateTime back to a string again. Try removing that part of the line.
In my case (.cshtml):
<td>#item.InvoiceDate.Value.ToShortDateString().ToString()</td>
Where InvoiceDtae is Nullable Date in DB
Related
I have to convert / parse a string into a DateTime
DateTime resultDate = new DateTime(2000,01,01);
string input = "24.24.2000";
string format = "dd.MM.yyyy";
bool success = DateTime.TryParseExact(input, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out resultDate);
now if the string is invalid and the conversion fails (in this example the 24 isn't a valid month) the Method TryParseExact changes the variable resultDate into DateTime.Min.
Why should I want to have DateTime.Min instead of the origin value if the conversion fails?
This isn't actually related to DateTime.TryParseExact nor is it unexpected behaviour. The result is an out parameter which means the method will always assign a value to it. The language actually requires the out keyword to avoid any confusion. out parameters are expected to be empty, which is why you can declare them without assigning a value.
A method's code can't even try to read an out parameter without initializing it to something. The following example will fail compilation with Use of unassigned out parameter 'result':
bool TryParse(string text,out DateTime result)
{
var original=result; //this fails compilation
result=DateTime.Today;
return true;
}
An out parameter is always assigned by the method unless the method throws an exception.
The documentation of DateTime.TryParseExact explains what the default value will be if conversion fails. It also explains that the parameter should be passed uninitialized
When this method returns, contains the DateTime value equivalent to
the date and time contained in s, if the conversion succeeded, or
MinValue if the conversion failed. The conversion fails if either the
s or format parameter is null, is an empty string, or does not contain
a date and time that correspond to the pattern specified in format.
This parameter is passed uninitialized.
Why should I want to have DateTime.Min instead of the origin value if the conversion fails
Because that is what the documentation states will happen.
When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed.
With a simple modification to your code you can take advantage of the result and not have to repeat your default value.
DateTime resultDate;
string input = "24.24.2000";
string format = "dd.MM.yyyy";
if(!DateTime.TryParseExact(input, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out resultDate))
resultDate = new DateTime(2000,01,01);
How to get short date for Get short date for System Nullable datetime (datetime ?)
for ed 12/31/2013 12:00:00 --> only should return 12/31/2013.
I don't see the ToShortDateString available.
You need to use .Value first (Since it's nullable).
var shortString = yourDate.Value.ToShortDateString();
But also check that yourDate has a value:
if (yourDate.HasValue) {
var shortString = yourDate.Value.ToShortDateString();
}
string.Format("{0:d}", dt); works:
DateTime? dt = (DateTime?)DateTime.Now;
string dateToday = string.Format("{0:d}", dt);
Demo
If the DateTime? is null this returns an empty string.
Note that the "d" custom format specifier is identical to ToShortDateString.
That function is absolutely available within the DateTime class. Please refer to the MSDN documentation for the class: http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
Since Nullable is a generic on top of the DateTime class you will need to use the .Value property of the DateTime? instance to call the underlying class methods as seen below:
DateTime? date;
String shortDateString;
shortDateString = date.Value.ToShortDateString();
Just be aware that if you attempt this while date is null an exception will be thrown.
If you want to be guaranteed to have a value to display, you can use GetValueOrDefault() in conjunction with the ToShortDateString method that other postelike this:
yourDate.GetValueOrDefault().ToShortDateString();
This will show 01/01/0001 if the value happened to be null.
Check if it has value, then get required date
if (nullDate.HasValue)
{
nullDate.Value.ToShortDateString();
}
Try
if (nullDate.HasValue)
{
nullDate.Value.ToShortDateString();
}
If you are using .cshtml then you can use as
<td>#(item.InvoiceDate==null?"":DateTime.Parse(item.YourDate.ToString()).ToShortDateString())</td>
or if you try to find short date in action or method in c# then
yourDate.GetValueOrDefault().ToShortDateString();
And is already answered above by Steve.
I have shared this as i used in my project. it works fine. Thank you.
I am using Visual Studio and I want to convert the string I have in my textbox into the DateTime format. I am using the function Convert.ToDateTime() but the value that is returned is the min value (0/0/0001 00:00:00).
What is the problem?
The code where I retrieve the string from my textbox.
//pass startdate end date to studentResult.aspx
Session["startdate"] = txtStartDate.Text.ToString();
Session["enddate"] = txtEndDate.Text.ToString();
The code where I convert the string to datetime format.
string startdate = (string)(Session["startdate"]);
string enddate = (string)(Session["enddate"]);
DateTime one = Convert.ToDateTime(startdate);
DateTime two = Convert.ToDateTime(enddate);
As pointed in my comment, Convert.ToDateTime method returns DateTime.MinValue if the parameter is null.
Return Value
Type: System.DateTime
The date and time equivalent of the value of value, or the date and time equivalent of DateTime.MinValue if value is null.
Probably your parameter is null but since you didn't gave us more details, we can't know what the exact problem is.
As a comman mistake, make sure that you are passing as a parameter .Text property of your TextBox, not itself. See Squid's comment.
use try parse, it'll return false if any conversion error occurs
Datetime #dateTime;
if(DateTime.TryParse(txtStartDate.Text, out #dateTime)) {
return #dateTime;
}
I am aware of the standard procedure for displaying a DateTime in a custom format, like so:
MessageBox.Show(dateSent.ToString("dd/MM/yyyy hh:mm:ss"));
However, when I change the variable from a DateTime to a DateTime? to accept null values, I lose the definition for the ToString(string) overload. I need to use DateTime? as I am reading from a database which potentially has null values - if the field in the database has a null value, then I need to assign the variable a null value too.
So I have two questions:
1) Out of curiosity, does anyone know if there is a reason why DateTime? does not contain an overload for ToString(string)?
2) Could anyone suggest an alternative method for what I am trying to achieve?
DateTime? is syntactic sugar for Nullable<DateTime> and that's why it don't have ToString(format) overload.
However, you can access underlying DateTime struct using Value property. But before that use HasValue to check, if the value exists.
MessageBox.Show(dateSent.HasValue ? dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") : string.Empty)
Instead of having to manually perform a null check every time, you can write an extension method.
public static string ToStringFormat(this DateTime? dt, string format)
{
if(dt.HasValue)
return dt.Value.ToString(format);
else
return "";
}
And use it like this (with whatever string format you want)
Console.WriteLine(myNullableDateTime.ToStringFormat("dd/MM/yyyy hh:mm:ss"));
You can still use
variableName.Value.ToString(customFormat);
I'm trying to convert a string to datetime to validate if user input is actually a date.
The error I'm getting is:
Cannot implicitly convert type bool to System.DateTime.
I've been looking online for a while and can't find anything specific enough to help me understand.
Code:
public bool is21YearsOfAge(string argument)
{
DateTime _parsedDateArgument;
DateTime convertStringToDate = System.DateTime.TryParse(argument, out >_parsedDateArgument);
if (convertStringToDate > DateTime.Now)
{
//do something
}
}
Thanks in advance.
The TryParse method returns a bool that informs you whether the parse was successful, rather than throwing an exception like the Parse method does. Try doing this:
DateTime convertStringToDate;
bool isDate = DateTime.TryParse(argument, out convertStringToDate);
If argument is a date, convertStringToDate will contain that date as a DateTime.
DateTime.TryParse returns bool to indicate if parsing was successeful. So you should do
System.DateTime.TryParse(argument, out _parsedDateArgument);
DateTime convertStringToDate =_parsedDateArgument
Look at the documentation for DateTime.TryParse - it returns a bool, but has an out parameter for the parsed result:
DateTime dateTime;
bool success = DateTime.TryParse(text, out dateTime);
If success is false, that means the text couldn't be parsed. (So typically at this point you'd display an error to the user.)
You've already got the out parameter - why did you expect to end up with two different DateTime values (one as the return value and one from the out parameter)?
When you get an error like this, always read the documentation as the first step towards diagnosing the problem.
It should be
DateTime convertStringToDate;
if(System.DateTime.TryParse(argument, out convertStringToDate))
{
//Now you will have converted date in convertStringToDate
if (convertStringToDate > DateTime.Now)
{
//do something
}
}
else
{
//argument not have a valid date
}
System.DateTime.TryParse will retrun true if, argument will have a valid date string to convert. and the converted date will be store in its out parameter.
DateTime.TryParse do not return a DateTime value. It returns a bool indicating if it could parse it.
Instead use
DateTime convertStringToDate;
if(DateTime.TryParse(argument, out convertStringToDate)){
//ok value is good
}else{
//Not ok value is not good
}
use this instead,
DateTime _parsedDateArgument;
bool success = System.DateTime.TryParse(argument, out _parsedDateArgument);
Always remember that Tryparse always return boolean.
TryParse returns a bool, use just Parse instead, or assign the out variable to the new you have:
System.DateTime.TryParse(argument, out _parsedDateArgument);
DateTime convertStringToDate = _parsedDateArgument;
or like this:
DateTime convertStringToDate = DateTime.Parse(argument);
add the following namespace
using System.Globalization;
Create object of CultureInfo class
CultureInfo MyCI = new CultureInfo("en-US");
DateTime convertStringToDate = System.DateTime.TryParse(argument.ToString("MM/dd/yy", MyCI), out _parsedDateArgument);