How to get only the date excluding time in asp.net c# - asp.net

How to get only the date excluding time in asp.net c#. I want only the date to be given as input to search like eg 3/11/2013

You can use DateTime.Date to get only date part of DateTime object
DateTime dateOnly = date1.Date;
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
If you have the Date in string and want to convert it to DateTime object first then you can use DateTime.ParseExact
result = DateTime.ParseExact("3/11/2013", "d/MM/yyyy", CultureInfo.InvariantCulture);

try to use this:
#{
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/yyyy");
}
Now to view just:
#date

DateTime dt = your_Dt.Date;
OR
You can format it into whatever format you want as below:
dt.Tostring("MM/dd/yyyy");
OR
You can convert the valued to shortdate as:
Convert.ToDateTime(your_Dt).ToShortDateString();

I saved the database as datetime in MS-SQL 2014, but when i need to only in date i do as below .... in cshtml
#foreach (var item in Model.dbModelLst)
{
<tr>
<td>#item.ChargeFrequency</td>
<td>#item.Charge</td>
<td>#item.FromDate.ToShortDateString().ToString()</td>
<td>#item.ToDate.ToShortDateString().ToString()</td>
<td>#item.Inv_SerialNo.SerialNo</td>
<td>#item.IncludeLic</td>
<td>#item.Status</td>
.....
.....
</tr>
}
Where dbModelist contain List of Model (IEnumerable)... I Solve this way. Thank you.

Related

ASP .Net Razor: format substract of 2 DateTime to a string specific format

I've got 1 model.DateTime value (which is I'm getting from Database) and I want to represent model DateTime which is end time, and DateTime.Now as now time.
I want to format subtract of that 2 DateTime-s as a specific String format "dd:hh:mm:ss".
In your ASP .Net view page you can open razor block and you can calculate difference between 2 variables that are DateTime type.
#{
DateTime end = (DateTime)item.close_date_time;
DateTime now = DateTime.Now;
TimeSpan diff = end.Subtract(now);
}
Then you can simply convert TimeSpan diff to string:
#diff.ToString(#"dd\:hh\:mm\:ss")
And by that I've solved time difference and to format it to specific string.
EDITED
Simply you can do this:
DateTime CurrentDateTime = DateTime.Now;
item.duration.Subtract(CurrentDateTime).ToString(#"dd\:hh\:mm\:ss");
Hope this will help

Format date from the following string in asp.net

I'm new to coding in asp.net and I'm struggling to format the date I've managed to pull from our CRM content pages' live date. I've wrote the following code that manages to pull and display the date the page was created:
TheDate = DR["LiveDate"].ToString();
But it formats the date and time like this:
13/11/2012 00:00:00
How can I modify my above code to just display the date like so:
Tuesday, 13 November 2012
Or, just without the time
13/11/2012
Thanks,
I assume that DR is a DataRow, isn't it?
If DR["LiveDate"] is already a DateTime column don't convert it to a localized string via ToString. Just cast it accordingly, for example via Field extension method:
DateTime dt = DR.Field<DateTime>("LiveDate");
If it's not a DateTime but a string you have to parse it to DateTime:
DateTime dt = DateTime.Parse(DR.Field<String>("LiveDate"));
If you want Tuesday, 13 November 2012 as result use:
string result = dt.ToString("dddd, dd MMMM yyyy", CultureInfo.InvariantCulture);
If you want 13/11/2012 use:
string result = dt.ToShortDateString();
or
string result = dt.ToString("d");
if your culture uses / as separator
otherwise
string result = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Further informations: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
for your second question, try
TheDate = DR["LiveDate"].ToString("dd/MM/yyyy")

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}" />

Date comparision using Linq

I have a DateTime type column named "CreatedDate" in my sql table, and am passing the value for this column by using "DateTime.Now" from my asp.net application....
The datas in my CreatedDate column are,
CreatedDate
-----------
2012-05-07 18:56:17.487
2012-05-07 18:56:28.443
2012-05-07 19:21:24.497
2012-05-14 15:22:04.587
I need to get the datas with this CreatedDate.
in my entity framework I tried the condition like
DataAccess.Entities dataEntities = new DataAccess.Entities();
DataAccess.Employee employee = dataEntities.Employees
.First(e => e.CreatedDate == DateTime.Today);
like this, I have data for this date(2012-05-14) , but the mininutes part differes (the DateTime.Today gives '2012-05-14 12:00:000' like this) here, and it shows error like, sequence contains no element....
How can I compare the 'Date' alone in Linq.....can anyone help me here,,,
Use the Date Property on the DateTime object
CreatedDate.Date==DateTime.Today
So your code will be
DataAccess.Employee employee=dataEntities.
Employees.First(e=>e.CreatedDate.Date==DateTime.Today);
Date Property returns the Date Component of the DateTime object and the time value set to 12:00:00 midnight (00:00:00).
Try this:
DataAccess.Employee employee =
dataEntities.Employees.First(e=>e.CreatedDate.Date==DateTime.Today)
I just declared two variable like
DateTime date1=DateTime.Now.Date;
DateTime date2=DateTime.Now.Date.AddDays(1);
and in the condition I used these variables like
DataAccess.Employee employee = dataEntities.Employees
.First(e => e.CreatedDate >= date1
&& e.CreatedDate < date2);
its working....

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());

Resources