扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-06-24 10:43:25
c#

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

扫码添加客服微信