C# 删除时间部分并以不同格式转换日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19540821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:14:37  来源:igfitidea点击:

Remove the time part and convert date time in different format

c#.netdatetimeformattype-conversion

提问by ankur

I have a date time object ,Whose time part I am extracting by this

我有一个日期时间对象,我由此提取的时间部分

var dateAndTime = Convert.ToDateTime(certificate.GetEffectiveDateString());
var date = dateAndTime.Date;

date is returning a value :10/23/2013 12:00:00 AMwhere is should only return 10/23/2013Don't know why the time part is not removed.

date 正在返回一个值:10/23/2013 12:00:00 AMwhere is 应该只返回10/23/2013不知道为什么没有删除时间部分。

After time part is removed, I need date to be converted in to this format October 23,2013.

删除时间部分后,我需要将日期转换为这种格式 October 23,2013

Can we remove the time part and change it into this format by some formats defined.

能不能把时间部分去掉,通过定义的一些格式改成这种格式。

采纳答案by Peter

There in no convertion in datetime. If you have a string to datetime, use a DateTime.TryParse. If you have a datetime and want a formatted string. You can use a format string:

日期时间没有转换。如果您有日期时间的字符串,请使用 DateTime.TryParse。如果您有日期时间并想要格式化的字符串。您可以使用格式字符串:

dateAndTime.ToString("MMMM dd,yyyy");

will give you a formatted date string.

会给你一个格式化的日期字符串。

回答by Smeegs

Here are all the standard date formats.

以下是所有标准日期格式。

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

回答by Neel

Use the Date property:

使用日期属性:

OR

或者

 var dateAndTime = DateTime.Now;
   dateAndTime.ToString("dd/MM/yyyy")

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

日期变量将包含日期,时间部分将为 00:00:00。

回答by BartoszKP

You probably mean that after ToStringthe time is rendered along with the date. Use ToShortDateStringmethod for example to get what you expect. There are many other possible ways to format a date as string, you can easily find them all here. The second format you mention can be achieved for example with:

您可能的意思是在ToString时间与日期一起呈现之后。使用ToShortDateString方法例如得到你所期望的。还有许多其他可能的方法将日期格式化为字符串,您可以在此处轻松找到它们。您提到的第二种格式可以通过以下方式实现:

date.ToString("MMMM dd,yyyy");

using this ToStringoverload.

使用这个ToString重载。

回答by Dzmitry Martavoi

If you want to retrieve date only string representation, you can use

如果您只想检索日期字符串表示,则可以使用

dateTime.ToString("d")

dateTime.ToString("d")

format, according to MSDN

格式,根据MSDN

回答by Tilak

Use following to get format October 23, 2013

使用以下获取格式 2013 年 10 月 23 日

dt.ToString("MMMM dd, yyyy");  

Date Time formats

日期时间格式

回答by Hexie

There are a few ways of doing this, the simplest and most effective, would be like so;

有几种方法可以做到这一点,最简单和最有效的就是这样;

DateTime.Now.Date.ToString("MMMM dd,yyyy") //Return October 23,2013

See this URLand this URLfor further reading on custom dates.

请参阅此URL和此URL以进一步阅读自定义日期。

回答by pcreech

The DateTime.Dateis just a DateTimewith the time portion set to zero, or midnight. If you are trying to output it as a string, then you can use either ToShortDateString()or one of the many various custom formats.

DateTime.Date只是DateTime时间部分设置为零或午夜的时间。如果您尝试将其作为字符串输出,那么您可以使用ToShortDateString()多种自定义格式之一或其中之一。

date.ToString("MMMM dd,yyyy")

should give you the output you are looking for.

应该给你你正在寻找的输出。

For information on DateTime.Date

有关信息 DateTime.Date

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

For more information on format strings for DateTime

有关格式字符串的更多信息 DateTime

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

回答by spacemonkey

// returns a date 'MM/dd/yyyy' string from a datetime string '2014-06-19 00:00:00.000'
public static string stripTime(string s) {
    string date = "";
    DateTime dt = DateTime.Parse(s);
    date = dt.Date.ToString("MM/dd/yyyy");
    return date;
}
// returns a date 'MM/dd/yyyy'string  from a datetime datetime 
public static string stripTime(DateTime d) {
    string date = "";
    date = d.Date.ToString("MM/dd/yyyy");
    return date;
}

回答by Luke T O'Brien

Surely this would work:

这肯定会起作用:

var dateAndTime = Convert.ToDateTime(certificate.GetEffectiveDateString());
var date = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);

I haven't tried myself however, but it should give the result you want. You could even create an Extension Method for it:

然而,我自己还没有尝试过,但它应该会给出你想要的结果。你甚至可以为它创建一个扩展方法:

public static class DateExtension
{
    public DateTime ToDateOnly(this DateTime d)
    {
       return new DateTime(d.Year, d.Month, d.Day);
    }
}