C# 日期格式 yyyy-MM-ddTHH:mm:ssZ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1728404/
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
date format yyyy-MM-ddTHH:mm:ssZ
提问by Saar
I assume this should be pretty simple, but could not get it :(.
In this format Z is time zone.
T is long time pattern
How could I get a date in this format except by using
我认为这应该很简单,但无法得到它:(。在这种格式中,Z 是时区
。T 是长时间模式
,除了使用
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm:ssZ"));
in C#
在 C# 中
采纳答案by Chris S
Using UTC
使用UTC
ISO 8601(MSDN datetime formats)
Console.WriteLine(DateTime.UtcNow.ToString("s") + "Z");
2009-11-13T10:39:35Z
2009-11-13T10:39:35Z
The Zis there because
该ž是因为存在
If the time is in UTC, add a 'Z' directly after the time without a space. 'Z' is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".
如果时间是 UTC,请在时间后直接添加一个“Z”,不要有空格。'Z' 是零 UTC 偏移的区域指示符。因此,“09:30 UTC”表示为“09:30Z”或“0930Z”。“14:45:15 UTC”将是“14:45:15Z”或“144515Z”。
If you want to include an offset
如果你想包括一个偏移量
int hours = TimeZoneInfo.Local.BaseUtcOffset.Hours;
string offset = string.Format("{0}{1}",((hours >0)? "+" :""),hours.ToString("00"));
string isoformat = DateTime.Now.ToString("s") + offset;
Console.WriteLine(isoformat);
Two things to note: + or - is needed after the time but obviously + doesn't show on positive numbers. According to wikipedia the offset can be in +hh format or +hh:mm. I've kept to just hours.
需要注意的两件事: + 或 - 在时间之后需要,但显然 + 不会显示在正数上。根据维基百科,偏移量可以是+hh 格式或+hh:mm。我一直坚持几个小时。
As far as I know, RFC1123(HTTP date, the "u" formatter) isn't meant to give time zone offsets. All times are intended to be GMT/UTC.
据我所知,RFC1123(HTTP 日期,“u”格式化程序)并不是要提供时区偏移量。所有时间均为 GMT/UTC。
回答by Pieter888
You could split things up, it would require more code but would work just the way you like it:
你可以把事情分开,它需要更多的代码,但会按照你喜欢的方式工作:
DateTime year = DateTime.Now.Year;
DateTime month = DateTime.Now.Month;
DateTime day = DateTime.Now.Day;
ect.
等。
finally:
最后:
Console.WriteLine(year+month+day+etc.);
This is a very bold way of handling it though...
不过,这是一种非常大胆的处理方式......
回答by archimed7592
回答by Rami A.
Console.WriteLine(DateTime.UtcNow.ToString("o"));
Console.WriteLine(DateTime.Now.ToString("o"));
Outputs:
输出:
2012-07-09T19:22:09.1440844Z
2012-07-09T12:22:09.1440844-07:00
回答by Vivek Kumar Samele
Single Line code for this.
单行代码。
var temp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ssZ");
回答by Khalil
One option could be converting DateTime to ToUniversalTime()before converting to string using "o"format. For example,
一种选择是在使用“o”格式转换为字符串之前将 DateTime 转换为ToUniversalTime()。例如,
var dt = DateTime.Now.ToUniversalTime();
Console.WriteLine(dt.ToString("o"));
It will output:
它会输出:
2016-01-31T20:16:01.9092348Z
回答by LucasM
"o" format is different for DateTime vs DateTimeOffset :(
DateTime 与 DateTimeOffset 的“o”格式不同:(
DateTime.UtcNow.ToString("o") -> "2016-03-09T03:30:25.1263499Z"
DateTimeOffset.UtcNow.ToString("o") -> "2016-03-09T03:30:46.7775027+00:00"
My final answer is
我的最终答案是
DateTimeOffset.UtcDateTime.ToString("o") //for DateTimeOffset type
DateTime.UtcNow.ToString("o") //for DateTime type
回答by HariKrishna
It works fine with Salesforce REST API query datetime formats
它适用于 Salesforce REST API 查询日期时间格式
DateTime now = DateTime.UtcNow;
string startDate = now.AddDays(-5).ToString("yyyy-MM-ddTHH\:mm\:ssZ");
string endDate = now.ToString("yyyy-MM-ddTHH\:mm\:ssZ");
//REST service Query
string salesforceUrl= https://csxx.salesforce.com//services/data/v33.0/sobjects/Account/updated/?start=" + startDate + "&end=" + endDate;
// https://csxx.salesforce.com/services/data/v33.0/sobjects/Account/updated/?start=2015-03-10T15:15:57Z&end=2015-03-15T15:15:57Z
It returns the results from Salesforce without any issues.
它从 Salesforce 返回结果,没有任何问题。
回答by Todd Menier
In C# 6+ you can use string interpolation and make this more terse:
在 C# 6+ 中,您可以使用字符串插值并使其更简洁:
$"{DateTime.UtcNow:s}Z"