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
Remove the time part and convert date time in different format
提问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 AM
where is should only return 10/23/2013
Don't know why the time part is not removed.
date 正在返回一个值:10/23/2013 12:00:00 AM
where 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.
以下是所有标准日期格式。
回答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 ToString
the time is rendered along with the date. Use ToShortDateString
method 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 ToString
overload.
使用这个ToString
重载。
回答by Dzmitry Martavoi
回答by Tilak
Use following to get format October 23, 2013
使用以下获取格式 2013 年 10 月 23 日
dt.ToString("MMMM dd, yyyy");
回答by Hexie
回答by pcreech
The DateTime.Date
is just a DateTime
with 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
回答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);
}
}