如何在datetime变量中存储日期? [英] How to store just date in datetime variable?
问题描述
我从datepicker获得了一个日期并将其存储到一个字符串变量中(因为它来自html文本框)
string to = Request.QueryString [ to];
之后我将日期的波斯数转换为英语
string d = Regex.Replace( from , [0-9],x = > (( char )(x。值[ 0 ] - ' 0' + ' 0'))。ToString());
和我转换日期类型(字符串到日期)。
DateTime dt = DateTime.ParseExact(d, yyyy / dd / MM,CultureInfo.InvariantCulture);
我尝试过的事情:
但是我给出了约会时间(我的约会时间没有任何时间)。
i给这个:
1395/04/01
i得到这个:
1/4/1395 12:00 :00 AM
i不想要时间。
i想要没有任何时间使用datetime变量。
任何想法?
您无法在DateTime中存储永恒日期:它不会像内部那样存储。你创建一个DateTime实例,它不会分别存储年,月和日 - 而是将它全部转换为自上一个特定点以来的一些刻度,并将其存储在UInt64值中。 />
因此,每个DateTime始终都有日期和时间组件。
您需要做的是在格式化数据时去掉Time组件相反显示。
见这里:格式化显示的日期时间 - 格式字符串描述 [ ^ ]
如果可以避免,请不要尝试比较基于字符串的日期:它可能导致如此多的复杂功能!
在进行永恒比较时,您可以使用Date属性从两侧去掉时间组件:
< pre lang =c#> if (startDate.Date < = endDate.Date)
{
if (startDate.Date < = DateTime.Now.Date)
{
if (startDate.Date < = DateTime.Today)
{自日期以来property(和Today)将Time重置为午夜,所有比较都有效并忽略时间组件。
我相信这应该有效:
DateTime date = yourDate.Date;
您可以这样显示:
控制台。 Writeline(date.ToString( d));
如果所有其他方法都失败了,你可以简单地创建一个函数,只要你需要日期就可以调用它来关闭时间。 。完全忽略它们。
i got a date from datepicker and store it in to one string variable(because it comes from html textbox)
string to = Request.QueryString["to"];
after that i convert the persian numbers of the date to english
string d = Regex.Replace(from, "[۰-۹]", x => ((char)(x.Value[0] - '۰' + '0')).ToString());
and i convert the type of date(string to date).
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?
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:
if (startDate.Date <= endDate.Date) {
if (startDate.Date <= DateTime.Now.Date) {
if (startDate.Date <= DateTime.Today) {Since the Date property (and Today) resets the Time to midnight, all comparisons work and ignore the time component.
I believe this should work:
DateTime date = yourDate.Date;
You can display it like this:
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.
这篇关于如何在datetime变量中存储日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!