Convert date as Integer in Codebehind - asp.net

I've been searching around the net but I only see integer to date. How can I convert a chosen date in radDatePicker to integer? I have tried Convert.ToInt32 but I get an error at runtime

You can try this code
DateTime dt =Convert.ToDateTime(RadDatePicker1.SelectedDate);
string temp = dt.ToShortDateString();
int value;
int.TryParse(temp, out value);

Try this:
DateTime.Now.Ticks
For normal datetime object it works. Means if you could get DateTime value from radDatePicker you may apply .Ticks property to it.

Related

How to insert null value for datetime field in asp.net?

I am trying to insert null value from front end. This is my code:
if (!string.IsNullOrEmpty(txtCallTwo.Text))
{
DateTime second = DateTime.ParseExact(txtCallTwo.Text.Trim(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
objclsConsultantLeadStatusProp.dtDate_2nd_Call = second;
}
else
{
DateTime second = DateTime.ParseExact(txtCallTwo.Text.Trim(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
objclsConsultantLeadStatusProp.dtDate_2nd_Call = null;
}
dtDate_2nd_Call property is declared as as Datetime.
You need to make dtDate_2nd_Call as nullable property. Syntax for nullable property is like this.
DateTime? dt = null;
For your case it should be Datetime? dtDate_2nd_Call instead of Datetime dtDate_2nd_Call
The answer to this question is pretty simple. I am not sure if it is of any help to you now...
The DateTime data structure in .NET does not accept null values and thus gives you the error, the simple solution to this is adding a question mark without spaces next to DateTime, i.e., DateTime?
DateTime? data structure accepts null values and thus you won't get the error.

Format DateTime Data Display In Grid View

I am trying to retrieve DateTime data type data from database in Asp.Net. Here is the code:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
However, the problem is the date I retrieved display in such format : 26/11/2013 12.00 AM . I just wanted to display the date but not the time. I'd tried to do this:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
string dateStr = date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
However, it does not works in my grid view. I wonder why is it so?
Thanks in advance.
Use the built-in to string function for jsut the Date value...
DateTime.ToShortDateString();
Select the corect type from database, so in this case datetime. Then you get a DateTime in your DataTable which you can cast with the DataRow's Field extension method:
DateTime date = dr.Field<DateTime>("packingDate");
If you only want to diplay the date:
string dateOnly = dr.Field<DateTime>("packingDate").ToShortDateString();
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("d");
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
You could format on the GridView control, for sample:
<asp:BoundField DataField="packingDate" HeaderText="PackingDate"
DataFormatString="{0:dd-M-yyyy}" />

How can I remove the Time from a DateTime value?

In my SQL database, I have a column formatted as DateTime and when I retrieve data from that column in ASP.NET, I catch it on the Date variable, than pass the value to textbox:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.ToString
but when I debug my website, the txtSchedDate.Text still gives me the full DateTime value:
7/17/2013 12:00:00 AM
is it possible to eliminate the time value here and just return the date?
Have you tried using something like
txtSchedDate.Text = Y.Date.ToString("MM/dd/yyyy")
or which ever format you wish to display.
Have a look at
DateTime.ToString Method (String)
Converts the value of the current DateTime object to its equivalent
string representation using the specified format.
Custom Date and Time Format Strings
Standard Date and Time Format Strings
Convert.ToDateTime(dt.Rows(0)("SCH_DATE")).ToString("M/d/yyy")
you can get date by txtSchedDate.Text = Y.Date.ToShortDateString()
Besides answers above, you can try converting it in SQL server
SELECT CONVERT(varchar(15), GETDATE(), 11)
Keep in mind after converting it's VARCHAR(15) instead of DATETIME.
Once you have a Date object, you can get the constituent pieces if you wish as well, like this:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.Year & "-" & Y.Date.Month & "-" & Y.Date.Day
Or you can use the custom and standard date and time format strings mentioned by others.

ASP.NET how to convert string to Date without forward slashes

Just wondering how I can convert the following string into a datetime 111222, at the moment it is telling me that this is not a valid datetime value..
You can use the DateTime.ParseExact method and supply the format your input is in.
Edit - added code:
DateTime dt = DateTime.ParseExact("111222", "yyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString());

how to convert string to datetime

I have a Session variable in which i have stored one date '22/7/2009'.Now i want to convert value in session to datetime. I tried folowing code:
Session("AppointmentDate") = Request.QueryString("ADate")
Dim s as datetime=Convert.ToDateTime(Session("AppointmentDate"))
But error is showing as 'string is not recognized as a valid datetime'.
Can anybody help me to convert value in Session to date?
Why don't you put the value into the session as a DateTime?
Failing that, use :-
Use :-
DateTime.ParseExact(
myDateString,
"d/M/yyyy",
System.Threading.Thread.CurrentThread.CurrentCulture);
Where myDateString contains your value from Session["AppointmentDate"]
Use DateTime.ParseExact to specify the date format string. I think it'd be something like:
dateValue = Date.ParseExact(dateString, "d", frFR, DateTimeStyles.None)
or
dateValue = Date.ParseExact(dateString, "dd/M/yyyy", enUS, DateTimeStyles.None)
I think you need to store your date as 22/07/2009 so that Convert.ToDateTime() could recognize it.

Resources