如何格式化日期时间 [英] How to format datetime
问题描述
我需要这种类型的输出
OutPut:CreatedDate:2017-11-01 11:22:22.931,ActiveOn:2017-11-01 11:22:22.931
公共类GetActivePro
{
public DateTime CreatedDate {get;组; }
public DateTime ActiveOn {get;组; }
}
我在我的类中使用dateTime变量,如果我在.ToString()中转换我的变量,则显示转换错误。
GetActivePro acpro = new GetActivePro();
acpro.CreatedDate = DateTime.Now.ToString(yyyy-MM-dd hh:mm:ss tt)
显示错误:
不能隐式地将类型字符串转换为system.datetime
现在我使用下面的代码,但输出格式不同,
我尝试过:
公共课GetActivePro
{
public DateTime CreatedDate {get;组; }
public DateTime ActiveOn {get;组; }
}
私有对象GetCustomDate()
{
GetActivePro acpro = new GetActivePro();
acpro.CreatedDate = DateTime.Now;
acpro.ActiveOn = DateTime.Now;
返回新
{
success = true,
Acpro = acpro
};
}
OutPut是:CreatedDate:20171101112222931,ActiveOn:20171101112222931
DateTime
不是字符串
所以只需:
acpro.CreatedDate = DateTime.Now;
如果您需要将DateTime显示为字符串,则转换为字符串:
< pre lang =c#> string str = acpro.CreatedDate.ToString( yyyy-MM-dd hh:mm:ss)
DateTime没有格式。它只是一个时刻,从过去的特定日期和时间点开始存储为多个刻度。
你想要做什么将DateTime值设置为格式化字符串:
public DateTime CreatedDate {get;组; }
acpro.CreatedDate = DateTime.Now.ToString(yyyy-MM-dd hh:mm:ss tt);你不能这样做,即使您将字符串解析回DateTime(使用TryParse或TryParseExact),它也不会保留其格式。
您将格式应用于日期时间你准备它输出,你不能在任何其他时间申请它!
acpro.CreatedDate = DateTime.Now;以后再使用ToString显示值。
const string DTFormat =yyyy-MM-dd hh:mm:ss.fff;
//样本用法:
//字符串dtformatted = DateTime.Now.ToString(DTFormat);
// Console.WriteLine(DateTime.Now.ToString(DTFormat));
I need this type of Output
OutPut: CreatedDate:2017-11-01 11:22:22.931, ActiveOn:2017-11-01 11:22:22.931
public class GetActivePro { public DateTime CreatedDate { get; set; } public DateTime ActiveOn { get; set; } }
I am using dateTime variable in my class , If I am Converting my variable in .ToString(), it showing conversion error.
GetActivePro acpro = new GetActivePro(); acpro.CreatedDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt")
showing error :
cannot implicitly convert type string to system.datetime
Now I am using following code but but output is in different format,
What I have tried:
public class GetActivePro { public DateTime CreatedDate { get; set; } public DateTime ActiveOn { get; set; } } private object GetCustomDate() { GetActivePro acpro = new GetActivePro(); acpro.CreatedDate = DateTime.Now; acpro.ActiveOn = DateTime.Now; return new { success = true, Acpro = acpro }; } OutPut is : CreatedDate:20171101112222931, ActiveOn:20171101112222931
DateTime
is not astring
so just do:
acpro.CreatedDate = DateTime.Now;
If you need to show the DateTime as a string then to the string conversion:
string str = acpro.CreatedDate.ToString("yyyy-MM-dd hh:mm:ss")
A DateTime doesn't have a format. It's just a "moment in time", which is stored as a number of ticks since a specific date and time point in the past.
What you are trying to do is set a DateTime value to a formatted string:
public DateTime CreatedDate { get; set; }acpro.CreatedDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt");You can't do that, and even if you parsed the string back to a DateTime (using TryParse or TryParseExact) it would not retain its formatting.
You apply formatting to a Datetime when you prepare it for output, you can't apply it at any other time!
acpro.CreatedDate = DateTime.Now;And later use the ToString to display the value.
const string DTFormat = "yyyy-MM-dd hh:mm:ss.fff"; // sample usage: // string dtformatted = DateTime.Now.ToString(DTFormat); // Console.WriteLine(DateTime.Now.ToString(DTFormat));
这篇关于如何格式化日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!