Skip to content

How to operate datetime format? #142

Closed

Description

@zyz

public static string ToJson(this object obj, NetJSONDateFormat dateFormat = NetJSONDateFormat.Default)
{
// 空对象返回
if (null == obj) return string.Empty;

        try
        {
            // 格式设置
            var jsonSetting = new NetJSONSettings
            {
                DateFormat = dateFormat
            };
            return NetJSON.NetJSON.Serialize(obj, jsonSetting);
        }
        catch (Exception ex)
        {
            throw new Exception("JsonHelper.ToJsonFast():" + ex.Message);
        }
    }

public static string ToJson2(this object obj, NetJSONDateFormat dateFormat = NetJSONDateFormat.Default)
{
// 空对象返回
if (null == obj) return string.Empty;

        try
        {
            // 格式设置    
            NetJSON.NetJSON.DateFormat = dateFormat;
            return NetJSON.NetJSON.Serialize(obj);
        }
        catch (Exception ex)
        {
            throw new Exception("JsonHelper.ToJsonFast():" + ex.Message);
        }
    }

public class Test
{
public int Id { get; set; }

    public string Name { get; set; }

    public DateTime CreateTime { get; set; }
}

var test = new Test
{
Id = 1,
Name = "Test",
CreateTime = DateTime.Now
};
var s1 = test.ToJson(NetJSONDateFormat.Default);
var s2 = test.ToJson(NetJSONDateFormat.EpochTime);
var s3 = test.ToJson(NetJSONDateFormat.ISO);
var s4 = test.ToJson(NetJSONDateFormat.JsonNetISO);

        var s5 = test.ToJson2(NetJSONDateFormat.Default);
        var s6 = test.ToJson2(NetJSONDateFormat.EpochTime);
        var s7 = test.ToJson2(NetJSONDateFormat.ISO);
        var s8 = test.ToJson2(NetJSONDateFormat.JsonNetISO);

Result:
s1 = s2 = s3 = s4 = ""
s5 = s6 = s7 = s8

if datetime type format "yyyy-MM-dd hh:mm:ss"
please can you tell me how to operate?

Activity

  1. rpgmaker commented on Dec 13, 2016

    @rpgmaker
    Owner

    A custom format such as yours is not supported. I will have to add it. The closest is ISO format.

  2. rpgmaker commented on Dec 14, 2016

    @rpgmaker
    Owner

    Did I answer your question?

  3. wmjordan commented on Dec 14, 2016

    @wmjordan
    Contributor

    @rpgmaker Is quite common for users running into a DateTime format like "yyyy-MM-dd hh:mm:ss". Is it possible to make the ISO format less strict by allowing the "T" in the pattern "yyyy-MM-dd'T'hh:mm:ss' to be another character?

  4. rpgmaker commented on Dec 14, 2016

    @rpgmaker
    Owner

    I suppose I can add an extension to support custom format instead of substitution of character. Though it will be easier to substitute it, don't want to create a custom ISO with character replacement only.

    Thanks,

  5. rpgmaker commented on Dec 26, 2016

    @rpgmaker
    Owner

    I have added DateStringFormat to the NetJSONSettings object. You can use it to specific how you want the formatting of string to be done and it will ignore the other options related to date. Note: this option uses the default .net date formatting method and will be slower than using the predefined option (This statement allow applies if you are worried about micro-perf of your application :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions