c#

c# litjson如何进行日期时间格式化

小樊
82
2024-06-24 10:43:25
栏目: 编程语言

在使用 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” 格式的字符串。您可以根据需要自定义日期时间格式。

0
看了该问题的人还看了