如何在时间+ 05:30的日期时间输出日期,而不是字符串 [英] How do I get output as date with time+05:30 in date time but not in string
本文介绍了如何在时间+ 05:30的日期时间输出日期,而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var dt = DateTime.Now.ToString("o");
DateTime date = DateTime.ParseExact(dt, "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(date);
Console.ReadLine();
我的尝试:
我得到的输出为2017-08-21T12:48:33 PM但我希望输出为2017-08-21T12:48:33.3060423 + 05:30(即日期时间不在字符串中)
What I have tried:
I am getting the output as 2017-08-21T12:48:33 PM but I want output as 2017-08-21T12:48:33.3060423+05:30 (i.e in date time not in string)
推荐答案
你不能。 DateTime值没有与之关联的格式 - 从特定时刻开始,它们纯粹是一些刻度。当它们被格式化为字符串以进行演示时,它们只能在年,月,日甚至毫秒的意义上获得任何格式。
要根据需要格式化DateTime:
You can't. DateTime values do not have a format associated with them - they are purely a number of ticks since a particular moment in time. They only acquire any format at all in the sense of years, month, days, or even milliseconds when they are formatted into a string for presentation.
To format the DateTime as you want try:
Console.WriteLine(date.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz"));
MM和SS更改为mm和ss - :O [/ edit]
[edit]MM and SS changed to mm and ss - :O [/edit]
对于简单的转换,您确定所有用户都在使用相同的当前文化设置:
For simple conversions where you are sure all your users are using the same current Culture settings:
using System.Globalization;
public string FormatIndiaTime(DateTime dt)
{
return dt.ToString("yyyy-MM-ddThh:mm:ss.ffffff+05:30");
}
// sample use in some method
var dtUTC = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Utc);
var dtIndia = TimeZoneInfo.ConvertTime(dtUTC, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
Console.WriteLine(FormatIndiaTime(dtIndia));
// sample output: 2017-10-22T05:10:39.494879+05:30
var utcFromIST = TimeZoneInfo.ConvertTimeToUtc(dtIndia, TimeZoneInfo.Utc);
您还在寻找更多吗?
这篇关于如何在时间+ 05:30的日期时间输出日期,而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文