How do I convert from a .NET DateTime to an IronPython datetime?

前端 未结 2 1314
名媛妹妹
名媛妹妹 2021-01-04 00:18

I\'m calling an IronPython script and passing it a .NET object that contains a DateTime structure.

I\'m trying to use IronPython\'s JSON support to seri

相关标签:
2条回答
  • 2021-01-04 00:34

    Anticipating that people might like to convert between these we actually make it really easy:

    import datetime
    from System import DateTime
    datetime.datetime(DateTime.Now)
    
    0 讨论(0)
  • 2021-01-04 00:55

    As we know, the datetime type has the following structure: datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]). So all you need is to find a way how to fulfill the required options.

    strptime is not yet implemented (otherwise you'd have the ability to simply call datetime.datetime.strptime(DateTime.Now.ToString(format), format).strftime(format)) in IronPython. Instead, you can use the following code (not very optimized one) for now:

    from System import DateTime
    
    import datetime
    
    d = DateTime.Now
    
    print datetime.date(d.Year, d.Month, d.Day)
    print datetime.datetime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second)
    
    0 讨论(0)
提交回复
热议问题