在使用 litjson 进行日期时间格式化时,可以使用 LitJson.JsonMapper.ToJson 方法将 DateTime 对象转换为 JSON 字符串时指定日期时间格式。
例如,可以使用以下代码将 DateTime 对象格式化为指定格式的字符串:
using LitJson;
using System;
class Program
{
static void Main()
{
DateTime dateTime = DateTime.Now;
JsonWriter writer = new JsonWriter();
writer.DateTimeFormat = "yyyy/MM/dd HH:mm:ss";
string jsonString = JsonMapper.ToJson(dateTime, writer);
Console.WriteLine(jsonString);
}
}
在上面的示例中,我们将当前时间的 DateTime 对象格式化为 “yyyy/MM/dd HH:mm:ss” 格式的字符串。您可以根据需要自定义日期时间格式。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c# string如何进行格式化输出