528

I need to remove time portion of date time or probably have the date in following format in object form not in the form of string.

06/26/2009 00:00:00:000

I can not use any string conversion methods as I need the date in object form.

I tried first converting the DateTime to a string, remove the time specific date from it, but it adds 12:00:00 AM as soon as I convert it back to DateTime object back again.

wonea
  • 3,987
  • 17
  • 71
  • 134
im useless
  • 6,311
  • 5
  • 32
  • 48

40 Answers40

937

Use the Date property:

var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;

The date variable will contain the date, the time part will be 00:00:00.

Vishal Suthar
  • 16,137
  • 2
  • 50
  • 97
driis
  • 151,614
  • 43
  • 262
  • 332
  • 38
    Wouldn't the time part be 12:00:00 AM as mentioned by the questioner? – FIre Panda May 25 '11 at 08:05
  • 16
    The time part is zero (midnight). When this is formatted as a string using the AM/PM format, this is represented as 12 AM. – driis May 25 '11 at 08:10
  • 108
    This does not answer the question. As the answer suggests, DateTime.Date (which is a DateTime it self!) has a time part defaulting to 00:00:00. The aswer should rather be: with DateTime, you can't. Construct your own structure, or have a look at Noda Time. – Tomas Vinter Sep 21 '12 at 15:11
  • 2
    DateTime.Now.Date is equvalent to DateTime.Today – Sergiu Mindras Nov 13 '13 at 10:33
  • 26
    @TomasVinter is correct. There is no "strictly-Date" structure in the .NET framework. You will have to create your own. However, DateTime exposes the .ToShortDateTime() method, which will format a string representing only the date portion of the value, and will format the date using the DateFormat configured in the current Culture (Thread.Current.CurrentCulture), so MM-dd-yyyy for US, dd-MMM-yyyy for EU, etc. – Michael J. Heier Dec 18 '13 at 23:26
  • You will always have a time in there; make sure you format it correctly so that the time doesnt show as an output; you could just show the day, month, year for example. Have a look at http://stackoverflow.com/questions/798121/date-vs-datetime –  Aug 21 '14 at 14:24
  • 19
    To those saying that this is not the correct answer: The answer is correct, but the question is wrong. It displays the questioner's ignorance of the subject. There is no format for the date to have in object form. Format is only meaningful when it is converted into a string. What is actually stored in his object is not `06/26/2009 00:00:00.000`, but `63381571200000000`, which is the number of Ticks (1/100000000th of a second) since `DateTime.MinValue` (01/01/0001). If you are going to display the date then convert it to a string, otherwise, it has a time component of midnight. Deal with it. – CptRobby Apr 29 '15 at 18:11
  • 1
    [Here](http://stackoverflow.com/a/39316624/2218697) is **another solution** using `String.Format`, hope helps someone. – Shaiju T Sep 04 '16 at 11:57
  • it is not acceptable answer. You must use String.Format to remove time – Munavvar Sep 21 '16 at 09:32
  • 1
    @CptRobby- A correct answer would explain that. This is not a correct answer. Simple as that. – Oliver Nicholls Aug 02 '19 at 10:01
193

You can use format strings to give the output string the format you like.

DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011

Read more about Custom date and time format strings.

0x49D1
  • 7,869
  • 11
  • 65
  • 119
123

Use the method ToShortDateString. See the documentation http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

var dateTimeNow = DateTime.Now; // Return 00/00/0000 00:00:00
var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000
ABM
  • 1,438
  • 2
  • 23
  • 39
Adriano Silva
  • 2,278
  • 1
  • 14
  • 29
38

Have a look at the DateTime.Date property.

Gets the date component of this instance.

Nick
  • 22,728
  • 6
  • 44
  • 79
31

The Date property will return the date at midnight.

One option could be to get the individual values (day/month/year) separately and store it in the type you want.

var dateAndTime = DateTime.Now; 
int year = dateAndTime.Year;
int month = dateAndTime.Month;
int day = dateAndTime.Day;

string.Format("{0}/{1}/{2}", month, day, year);
Adrian
  • 2,735
  • 1
  • 19
  • 30
NoviceProgrammer
  • 3,189
  • 1
  • 19
  • 31
24

None of the above answers solved my problem on winforms.

the easiest way to reach ONLY date is the simple function in Datetime:

DateTime dt = DateTime.now;
String BirthDate = dt.ToShortDateString();

You will only have date in Birthday string .

MehraD
  • 419
  • 3
  • 15
16

Try to make your own Structure for that. DateTime object will have date and time both

Umesh CHILAKA
  • 1,446
  • 14
  • 25
  • 3
    This should be the answer. It appears to be the only answer to the actual question - the time can't be removed from the object. Roll your own, live with the midnight (or time of the day) or work with a ToString representation of what you want. – Phil Cooper Mar 02 '14 at 07:09
  • This may be the most correct answer but is also completely academic for all but those with no life, who like reinventing a wheel that turns very nicely on its own. If we are really worried about the added memory used storing an additional long property with our DateTime struct, we have bigger problems, than simply ignoring it. – iGanja Feb 28 '15 at 00:51
  • Thanks for the answer. Without creating a Structure, the user can use a string variable in most of the cases. What do you think? – Kushan Randima Dec 18 '19 at 01:26
  • @KushanRandima, this is true in most of the cases. However, in the question user does not want in string format hence I suggested this as one of the alternates. – Umesh CHILAKA Dec 25 '19 at 08:53
15

You can't. A DateTime in .NET always have a time, defaulting to 00:00:00:000. The Date property of a DateTime is also a DateTime (!), thus having a time defaulting to 00:00:00:000 as well.

This is a shortage in the .NET Framework, and it could be argued that DateTime in .NET violates the Single Responsibility Principle.

Tomas Vinter
  • 2,610
  • 7
  • 34
  • 45
  • This is the correct answer for this question. DateTime objects will always have Time. – Drkawashima Apr 25 '13 at 10:49
  • 1
    Out of interest, why would you consider this a violation of SRP? If you consider the responsibility is representing a point in time and the class exposes the underlying Ticks property, the Year/Hour/Millisecond properties are purely there to deal with (mainstream) convention. – Rob Church May 02 '13 at 13:56
  • Because date and time are two different concepts, unfortunately in the .NET Framework bundled into the same structure. Thus, the structure has more than one responsibility (handling both date AND time), and therefore violates SRP. DateTime in .NET does not just represent a point in time, but many other things as well. Hence, considering it's responsibility to only representing a point in time would be odd. – Tomas Vinter Aug 26 '13 at 14:27
  • 3
    a point in time, is represented by both date and time. If you tell someone to meet you at 1:00 PM, you are making a huge assumption the person knows what day. Even if today is the day, precision dictates that be explicitly stated. Ergo a single value representing a point in time with any precision MUST include both the date and the time using a reference starting point. Therefore date, from a starting reference, and time, from a starting reference, are clearly the SAME concept; time adding additional precision. – iGanja Feb 28 '15 at 00:45
  • 1
    Time alone is useful when you are talking about multiple instances: eg every day at midnight UTC; on the first of every month at noon local time; etc. Arguably, TimeSpan can be used to represent that via convention, but it may be cleaner to have a Time object that represents a point within a 24 hour period and a time zone. Ultimately, there are 3 ideas that DateTime encapsulates: Date, Time, & TimeZone. – David Beckman Oct 05 '16 at 21:21
  • Agree with @DavidBeckman. Except he left out a 4th idea, the one the Type is named after: Date-Time. Also, he left out an example of the Date idea. It could be a date that payment must be charged (no sooner and no later than but at any time during that date) or a date that a payment or project is due by the "close of office hours at your closest applicable office" (which may vary between office, region, state, etc. but the date is just representing the national or even international policy). While I would lean towards TimeSpan for the Time idea, that wouldn't be practical for a Date idea. – Tom May 14 '19 at 19:45
  • For a Date Class, I would create one that uses a `DateTime` Class internally for storage but has Constructors that won't accept a time portion, and for interoperability with the much more common `DateTime` Class libraries, has a `Date(DateTime dateTime)` or `FromDateTime(DateTime dateTime)` Constructor (that Throws an Exception if the Time is not 12:00 am) and a `ToDateTime` Property. – Tom May 14 '19 at 19:50
12

DateTime.Date

var newDate = DateTime.Now; //newDate.Date property is date portion of DateTime
Andrew
  • 13,934
  • 8
  • 78
  • 93
VikciaR
  • 3,074
  • 16
  • 30
10

Here is another method using String.Format

    DateTime todaysDate = DateTime.UtcNow;

    string dateString = String.Format("{0:dd/MM/yyyy}", todaysDate);

    Console.WriteLine("Date with Time: "+ todaysDate.ToString());

    Console.WriteLine("Date Only : " + dateString);

Output:

Date with Time: 9/4/2016 11:42:16 AM

Date Only : 04/09/2016

This also works if the Date Time is stored in database.

For More Date and Time formatting check these links:

Reference 1

Reference 2

Hope helps.

Shaiju T
  • 5,220
  • 15
  • 91
  • 175
  • The OP explicitly states "I can not use any string conversion methods as I need the date in object form." – ChrisF Sep 07 '16 at 14:23
9

The easiest way is something like this and it will return only the date:

var date = DateTime.Now.ToShortDateString();
iknow
  • 4,796
  • 9
  • 19
  • 37
flk
  • 121
  • 1
  • 2
7

This way of get only date without time

DateTime date = DateTime.Now;
string Strdateonly = date.ToString("d");

Output = 5/16/2015

gsamaras
  • 66,800
  • 33
  • 152
  • 256
Mayank Gupta
  • 81
  • 1
  • 5
  • 1
    Please consider, that a late answer is worth adding (and from other users perspective upvote), only if it is distinctly different from other answers. What is not a case here. – m.cekiera May 16 '15 at 13:37
5

I'm surprised no one has mentioned DateTime.Today

var date = DateTime.Today;
// {7/1/2014 12:00:00 AM}

See MSDN

LukeF
  • 51
  • 1
  • 3
  • +1 only because your answer produces a date with the correct properties, however the OP wants to remove the time from an existing date, not create a new date. – iGanja Feb 28 '15 at 00:54
5

I wrote a DateOnly structure. This uses a DateTime under the skin but no time parts are exposed publically:

using System;

public struct DateOnly : IComparable, IFormattable, IComparable<DateOnly>, IEquatable<DateOnly>
{

    private DateTime _dateValue;

    public int CompareTo(object obj)
    {
        if (obj == null)
        {
            return 1;
        }

        DateOnly otherDateOnly = (DateOnly)obj;
        if (otherDateOnly != null)
        {
            return ToDateTime().CompareTo(otherDateOnly.ToDateTime());
        }
        else
        {
            throw new ArgumentException("Object is not a DateOnly");
        }
    }

    int IComparable<DateOnly>.CompareTo(DateOnly other)
    {
        return this.CompareToOfT(other);
    }
    public int CompareToOfT(DateOnly other)
    {
        // If other is not a valid object reference, this instance is greater.
        if (other == new DateOnly())
        {
            return 1;
        }
        return this.ToDateTime().CompareTo(other.ToDateTime());
    }

    bool IEquatable<DateOnly>.Equals(DateOnly other)
    {
        return this.EqualsOfT(other);
    }
    public bool EqualsOfT(DateOnly other)
    {
        if (other == new DateOnly())
        {
            return false;
        }

        if (this.Year == other.Year && this.Month == other.Month && this.Day == other.Day)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static DateOnly Now()
    {
        return new DateOnly(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
    }

    public static bool TryParse(string s, ref DateOnly result)
    {
        DateTime dateValue = default(DateTime);
        if (DateTime.TryParse(s, out dateValue))
        {
            result = new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
            return true;
        }
        else
        {
            return false;
        }
    }

    public static DateOnly Parse(string s)
    {
        DateTime dateValue = default(DateTime);
        dateValue = DateTime.Parse(s);
        return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
    }

    public static DateOnly ParseExact(string s, string format)
    {
        CultureInfo provider = CultureInfo.InvariantCulture;
        DateTime dateValue = default(DateTime);
        dateValue = DateTime.ParseExact(s, format, provider);
        return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
    }

    public DateOnly(int yearValue, int monthValue, int dayValue) : this()
    {
        Year = yearValue;
        Month = monthValue;
        Day = dayValue;
    }

    public DateOnly AddDays(double value)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddDays(value);
        return new DateOnly(d.Year, d.Month, d.Day);
    }

    public DateOnly AddMonths(int months)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddMonths(months);
        return new DateOnly(d.Year, d.Month, d.Day);
    }

    public DateOnly AddYears(int years)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddYears(years);
        return new DateOnly(d.Year, d.Month, d.Day);
    }

    public DayOfWeek DayOfWeek
    {
        get
        {
            return _dateValue.DayOfWeek;
        }
    }

    public DateTime ToDateTime()
    {
        return _dateValue;
    }

    public int Year
    {
        get
        {
            return _dateValue.Year;
        }
        set
        {
            _dateValue = new DateTime(value, Month, Day);
        }
    }

    public int Month
    {
        get
        {
            return _dateValue.Month;
        }
        set
        {
            _dateValue = new DateTime(Year, value, Day);
        }
    }

    public int Day
    {
        get
        {
            return _dateValue.Day;
        }
        set
        {
            _dateValue = new DateTime(Year, Month, value);
        }
    }

    public static bool operator == (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() == aDateOnly2.ToDateTime());
    }

    public static bool operator != (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() != aDateOnly2.ToDateTime());
    }

    public static bool operator > (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() > aDateOnly2.ToDateTime());
    }

    public static bool operator < (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() < aDateOnly2.ToDateTime());
    }

    public static bool operator >= (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() >= aDateOnly2.ToDateTime());
    }

    public static bool operator <= (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() <= aDateOnly2.ToDateTime());
    }

    public static TimeSpan operator - (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() - aDateOnly2.ToDateTime());
    }


    public override string ToString()
    {
        return _dateValue.ToShortDateString();
    }

    public string ToString(string format)
    {
        return _dateValue.ToString(format);
    }

    public string ToString(string fmt, IFormatProvider provider)
    {
        return string.Format("{0:" + fmt + "}", _dateValue);
    }

    public string ToShortDateString()
    {
        return _dateValue.ToShortDateString();
    }

    public string ToDbFormat()
    {
        return string.Format("{0:yyyy-MM-dd}", _dateValue);
    }
}

This is converted from VB.NET, so apologies if some conversions are not 100%

Matt Wilko
  • 25,893
  • 10
  • 85
  • 132
5

Use date.ToShortDateString() to get the date without the time component

var date = DateTime.Now
var shortDate = date.ToShortDateString() //will give you 16/01/2019

use date.ToString() to customize the format of the date

var date = DateTime.Now
var shortDate = date.ToString('dd-MMM-yyyy') //will give you 16-Jan-2019
Yaobin Then
  • 2,334
  • 1
  • 27
  • 50
3

You Can Try This for the Only Date From the Datetime

String.Format("{0:d/M/YYYY}",dt);

Where dt is the DateTime

Vishal Suthar
  • 16,137
  • 2
  • 50
  • 97
Vikas
  • 358
  • 2
  • 3
  • 8
3

Came across this post when trying to solve the original Q.

I am using Asp.Net and after some research I have found when you are binding to the value of the date in code behind, you can drop the time so it will not display on screen.

C#:

DateTime Today = DateTime.Now;

aspx:

<%: this.Today.ToShortDateString() %>
hmnzr
  • 1,370
  • 1
  • 14
  • 23
John
  • 3,633
  • 18
  • 62
  • 135
3

use

DateTime.Now.ToString("dd-MM-yyyy");
James
  • 162
  • 1
  • 10
3

If you are converting it to string, you can easily do it like this.

I'm taking date as your DateTime object.

date.ToString("d");

This will give you only the date.

TAS
  • 311
  • 2
  • 10
2
string dt = myCalender.SelectedDate.ToString();
string date = dt.Remove(10);
displayDate.Content = date;

If you take date from calender, with this we also get time. Which is not required all time. Using this we can remove time from date.

Taryn
  • 224,125
  • 52
  • 341
  • 389
hizbul25
  • 3,519
  • 4
  • 21
  • 35
2

Declare the variable as a string.

example :

public string dateOfBirth ;

then assign a value like :

dateOfBirth = ((DateTime)(datetimevaluefromDB)).ToShortDateString();
codeMagic
  • 43,753
  • 13
  • 72
  • 91
Saud Khan
  • 21
  • 1
  • people should read the question, and stop converting structs (on the stack) to objects (on the heap) for no reason. – iGanja Feb 28 '15 at 00:58
2

Create a struct that holds only the properties you want. Then an extension method to easily get that struct from an instance of DateTime.

public struct DateOnly
{
    public int Day { get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
}

public static class DateOnlyExtensions
{
    public static DateOnly GetDateOnly(this DateTime dt)
    {
        return new DateOnly
        {
            Day = dt.Day,
            Month = dt.Month,
            Year = dt.Year
        };
    }
}

Usage

DateTime dt = DateTime.Now;
DateOnly result = dt.GetDateOnly();
mason
  • 28,517
  • 9
  • 66
  • 106
2

I know this is an old post with many answers, but I haven't seen this way of removing the time portion. Suppose you have a DateTime variable called myDate, with the date with time part. You can create a new DateTime object from it, without the time part, using this constructor:

public DateTime(int year, int month, int day);

Like this:

myDate = new DateTime(myDate.Year, myDate.Month, myDate.Day);

This way you create a new DateTime object based on the old one, with 00:00:00 as time part.

Guillermo Gutiérrez
  • 15,624
  • 15
  • 82
  • 108
2

This could be simply done this way:

var dateOnly = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day)
HichemSeeSharp
  • 3,055
  • 2
  • 16
  • 44
2

To get only the date portion use the ToString() method,

example: DateTime.Now.Date.ToString("dd/MM/yyyy")

Note: The mm in the dd/MM/yyyy format must be capitalized

nate.py
  • 21
  • 2
2

You can use this simple code below.

Code: DateTime.Now.ToShortDateString();

Ex. Console.WriteLine(DateTime.Now.ToShortDateString());

Ian
  • 21
  • 1
2

in my experience none of the said solutions worked, maybe because I wanted to remove the time from extracted date from database, but the code below worked fine:

var date = target_date.Value.ToString("dd/MM/yyyy"); 
Vishal Suthar
  • 16,137
  • 2
  • 50
  • 97
Daniel
  • 3,032
  • 4
  • 25
  • 36
  • The question specifically stated that the date needed to be an object, not a string. This solution returns a string. – saluce Jun 13 '12 at 14:27
1

Use .Date of a DateTime object will ignore the time portion.

Here is code:

DateTime dateA = DateTime.Now;
DateTime dateB = DateTime.Now.AddHours(1).AddMinutes(10).AddSeconds(14);
Console.WriteLine("Date A: {0}",dateA.ToString("o"));
Console.WriteLine("Date B: {0}", dateB.ToString("o"));
Console.WriteLine(String.Format("Comparing objects A==B? {0}", dateA.Equals(dateB)));
Console.WriteLine(String.Format("Comparing ONLY Date property A==B? {0}", dateA.Date.Equals(dateB.Date)));
Console.ReadLine();

Output:

>Date A: 2014-09-04T07:53:14.6404013+02:00
>Date B: 2014-09-04T09:03:28.6414014+02:00
>Comparing objects A==B? False
>Comparing ONLY Date property A==B? True
Jota
  • 11
  • 1
1

Use a bit of RegEx:

Regex.Match(Date.Now.ToString(), @"^.*?(?= )");

Produces a date in the format: dd/mm/yyyy

DividedByZero
  • 4,189
  • 2
  • 17
  • 31
1

For using by datalist, repeater.. in aspx page:<%# Eval("YourDateString").ToString().Remove(10) %>

1
static void Main(string[] args)
{
    string dateStrings =  "2014-09-01T03:00:00+00:00" ;

    DateTime convertedDate = DateTime.Parse(dateStrings);
    Console.WriteLine("  {0} ----------------- {1}",

    convertedDate,DateTime.Parse(convertedDate.ToString()).ToString("dd/MM/yyyy"));

    Console.Read();
}
roemel
  • 3,120
  • 4
  • 24
  • 48
ImranNaqvi
  • 2,332
  • 3
  • 27
  • 64
1

This code gives you a clear view of writing Date as well as Time separately

string time = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00");
        string date = DateTime.Now.ToString("M-dd-yyyy");
        MessageBox.Show(date + "\n" + time);

Hope this helps.

Zero
  • 327
  • 2
  • 5
  • 20
1

Getting the Date part of a DateTime object didn't workout for me because I'm working on the client-side and the returned web service values have some null dates. As a result, it tries to get the Date part of a null value and it throws a runtime exception. The following example is how I solved my problem:

string dt = employer.BirthDay.ToString();
if(dt == ""){ dt = "N/A";}
else dt = dt.Substring(0,10);
  1. Get the DateTime value as string into a string variable.
  2. Check if it's null. If null, assign a string variable.
  3. If not null, get the first 10 characters of the string DateTime value and assign it to the string variable.

I'm sharing this for future reference.

apollo
  • 387
  • 2
  • 9
0
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/YYYY");
James
  • 162
  • 1
  • 10
0

If you want to remove part of time from a DateTime, try using this code:

DateTime dt = new DateTime();

dt = DateTime.Now; //ex: 31/1/2017 6:30:20 PM

TimeSpan remainingTime = new TimeSpan(0, dt.Hour - 5, dt.Minute - 29, dt.Second - 19);
dt=dt.Add(remainingTime);

label1.Text = dt.ToString("HH:mm:ss"); // must be HH not hh

the output : 01:01:01

ryanyuyu
  • 5,999
  • 10
  • 46
  • 47
Na'il
  • 29
  • 9
  • `DateTime` class already has methods for removing time from a date, like `dt.AddHours(-5)` or `fim.AddMinutes(-2)`, which returns a **new** `DateTime` object which that date (5 hours earlier or 2 minutes earlier respectively). Also, by using `dt.Add()`, you are *incresing* `dt` (which refers to **now** since you did `dt = DateTime.Now`) with a time span, and not removing. So in your case, `dt` would refer to *31/1/2017 7:31:01 PM* (since `remainingTime` would be a time span of 1h01m01s). – Alisson Jan 31 '17 at 15:59
0

Try this, if you use a DateTimeOffset, it will also take care of the timezone

date1 = date1.LocalDateTime.Date;

If you need to add hours, use this:

date1 = date1.LocalDateTime.Date;
date1 = date1.AddHours(23).AddMinutes(59).AddSeconds(59);
iwhp
  • 823
  • 6
  • 16
0

I think you would this: DateTime onlyDate = DateTime.Today.Date; or, that's the same DateTime onlyDate = yourDateTime.Date; So use the property Date.

0

In case you would want to use Binding and show only Date portion without time

ToolTip="{Binding TransactionModel.TransactionDate, StringFormat=d}"

Kovalenko Ihor
  • 101
  • 1
  • 5
0

Add Date property to the DateTime variable

var dateTime = DateTime.Now
var onlyDate = dateTime.Date

Or You can use DataType annotation as well.

[DataType(DataType.Date)]
public DateTime dateTime {get; set;}

The DataType annotation is inside the System.ComponentModel.DataAnnotations namespace.

Naveed Hematmal
  • 133
  • 1
  • 14
-1

Just use one line of code:

var dateAndTime = DateTime.Now.Date;
Pang
  • 8,605
  • 144
  • 77
  • 113
NinjaCoder
  • 89
  • 9