Have datetime.now return to the nearest second

前端 未结 6 862
眼角桃花
眼角桃花 2021-01-04 04:38

I have a \"requirement\" to give a timestamp to the nearest second... but NOT more accurate than that. Rounding or truncating the time is fine.

I have come up with t

6条回答
  •  长情又很酷
    2021-01-04 04:52

    You can use this constructor:

    public DateTime(
        int year,
        int month,
        int day,
        int hour,
        int minute,
        int second
    )
    

    so it would be:

    DateTime dt = DateTime.Now;
    DateTime secondsDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    

提交回复
热议问题