鉴于:
DateTime.UtcNow
Run Code Online (Sandbox Code Playgroud)
如何获得符合ISO 8601格式的相同值的字符串?
请注意,ISO 8601定义了许多类似的格式.我正在寻找的具体格式是:
yyyy-MM-ddTHH:mm:ssZ
Run Code Online (Sandbox Code Playgroud) 有这个示例代码,但它开始讨论毫秒/纳秒问题.
自从C#中的Unix纪元以来,同样的问题出现在MSDN上,秒.
这是我到目前为止所得到的:
public Double CreatedEpoch
{
get
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
TimeSpan span = (this.Created.ToLocalTime() - epoch);
return span.TotalSeconds;
}
set
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
this.Created = epoch.AddSeconds(value);
}
}
Run Code Online (Sandbox Code Playgroud) 你如何使用jquery和mvc3将日期时间(我需要它到第二个)传递给c#.这就是我所拥有的
var date = new Date();
$.ajax(
{
type: "POST",
url: "/Group/Refresh",
contentType: "application/json; charset=utf-8",
data: "{ 'MyDate': " + date.toUTCString() + " }",
success: function (result) {
//do something
},
error: function (req, status, error) {
//error
}
});
Run Code Online (Sandbox Code Playgroud)
我不知道日期应该是什么格式,让C#理解它.
我已全局明确配置我的MVC4应用程序以使用JSON.NET序列化程序.我知道在序列化日期时我可以选择使用ISO标准日期或旧的Microsoft日期格式.
但是我如何输出我自己的自定义dateTime格式字符串,如:"dd/MM/yyyy hh:mm".
我在插入Json.NET作为默认的序列化程序时可以在MVC3中执行此操作,但似乎无法在MVC4中执行此操作.
到目前为止我在application_start中做过:
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
Formatting = Formatting.Indented,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
};
jSettings.Converters.Add(new MyDateTimeConvertor() );
settings = jSettings;
Run Code Online (Sandbox Code Playgroud)
和我试图暗示的自定义转换器是这样的:
public class MyDateTimeConvertor : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return DateTime.Parse(reader.Value.ToString());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((DateTime)value).ToString("dd/MM/yyyy hh:mm"));
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激 :)
我正在将Web API从.NET Core 2.2迁移到3.0,并希望使用新的API System.Text.Json。使用时,Newtonsoft我可以DateTime使用以下代码进行格式化。我该怎么做?
.AddJsonOptions(options =>
{
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";
});
Run Code Online (Sandbox Code Playgroud) 这段代码:
public class PhotoDescriptor
{
public DateTime DateCreatedUtc { get; set; }
}
public class PhotosController : ApiController
{
// GET api/photos
public IEnumerable<PhotoDescriptor> GetListOfPhotos()
{
return new PhotoDescriptor[]
{
new PhotoDescriptor
{
DateCreatedUtc = DateTime.ParseExact(
"2012-07-24T00:28:41.8738770Z",
"o",
CultureInfo.InvariantCulture,
DateTimeStyles.None).ToUniversalTime(),
},
new PhotoDescriptor
{
DateCreatedUtc = DateTime.ParseExact(
"2012-07-24T00:28:41.0000000Z",
"o",
CultureInfo.InvariantCulture,
DateTimeStyles.None).ToUniversalTime(),
},
};
}
Run Code Online (Sandbox Code Playgroud)
返回以下JSON:
[{"DateCreatedUtc":"2012-07-24T00:28:41.873877Z"},
{"DateCreatedUtc":"2012-07-24T00:28:41Z"}]
Run Code Online (Sandbox Code Playgroud)
请注意,从日期时间中删除了尾随零.但是,当我试图解析这些字符串以获取我的DateTime时,我得到FormatException - String was not recognized as a valid DateTime:
var date = DateTime.ParseExact("2012-07-24T00:28:41.873877Z", "o", CultureInfo.InvariantCulture, DateTimeStyles.None);
Run Code Online (Sandbox Code Playgroud)
这是正确的,根据MSDN DateTime.ParseExact方法: …
我有一个Winform客户端,它向我的控制器发送一个json post请求,其日期时间值为格式dd/MM/yyyy,并且该调用返回状态代码400.
我试着添加:
<globalization uiCulture="fr" culture="fr-FR" />
Run Code Online (Sandbox Code Playgroud)
到我的web.config文件,但它仍然无法正常工作.
编辑:我还应该添加我无法控制客户端,我添加:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
Run Code Online (Sandbox Code Playgroud)
我的模型没有结果