Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got a date from datepicker and store it in to one string variable(because it comes from html textbox)
C#
string to = Request.QueryString["to"];

after that i convert the persian numbers of the date to english
C#
string d = Regex.Replace(from, "[۰-۹]", x => ((char)(x.Value[0] - '۰' + '0')).ToString());

and i convert the type of date(string to date).
C#
DateTime dt = DateTime.ParseExact(d, "yyyy/dd/MM", CultureInfo.InvariantCulture);


What I have tried:

but i give time with the date(my date has not any time).
i give this :
۱۳۹۵/۰۴/۰۱
i get this :
1/4/1395 12:00:00 AM
i dont want the time.
i want use the datetime variable without any time.
any idea?
Posted
Updated 15-Jul-16 2:51am
Comments
an0ther1 14-Jul-16 23:52pm    
A DateTime variable always includes a time component - if the time component is not set it is assumed to be 12.00 AM
What are you trying to do? If you want to view the Date part of a your DateTime variable you need to convert it to a string - you do not really look at DateTime values just string representations of them.
DateTime.ToString("dd/MM/yyyy"); will give you the value as a string containing just the day, month and year separated by "/"

You can't store a "timeless" date in a DateTime: it isn't stored like that internally. Wen you create a DateTime instance, it doesn't store the year, month, and day separately - instead it converts it all to a number of ticks since a specific point in the the past, and stores that in a UInt64 value.
As a result, every DateTime has both a "date and a time component" at all times.
What you need to do is strip out the Time component when you format the data for display instead.
See here: Formatting a DateTime for display - format string description[^]

Never try to compare string based dates if you can avoid it: it can cause so many complications!
When doing "timeless" comparisons, you can strip out the time component from both sides using the Date property:
C#
if (startDate.Date <= endDate.Date)
   {

C#
if (startDate.Date <= DateTime.Now.Date)
   {

C#
if (startDate.Date <= DateTime.Today)
   {
Since the Date property (and Today) resets the Time to midnight, all comparisons work and ignore the time component.
 
Share this answer
 
I believe this should work:
C#
DateTime date = yourDate.Date;

You can display it like this:
C#
Console.Writeline(date.ToString("d"));

If all else fails, you can simply create a function that you can cal whenever you need the date in order to chop the times off...simply ignore them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900