Expand description

Ser/de to/from optional timestamps in milliseconds

Intended for use with serdes with attribute.


use chrono::serde::ts_milliseconds_option;
#[derive(Deserialize, Serialize)]
struct S {
    #[serde(with = "ts_milliseconds_option")]
    time: Option<DateTime<Utc>>

let time = Some(Utc.ymd(2018, 5, 17).and_hms_milli(02, 04, 59, 918));
let my_s = S {
    time: time.clone(),

let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918}"#);
let my_s: S = serde_json::from_str(&as_string)?;
assert_eq!(my_s.time, time);


  • Deserialize a DateTime from a millisecond timestamp or none
  • Serialize a UTC datetime into an integer number of milliseconds since the epoch or none