简体   繁体   中英

get DateTime.UtcNow.Ticks older than a day

I am saving images with unique names. To get unique name I used DateTime.UtcNow.Ticks . Now I want to delete all images older than a day. How can I get the ticks older than a day? I have mapped time with ticks.

1:52:14.312 PM - 633614215343125000
1:52:14.359 PM - 633614215343593750
1:52:14.421 PM - 633614215344218750
1:52:14.468 PM - 633614215344687500
1:52:14.515 PM - 633614215998593750

What is the best way to get ticks older than a day? OR how to get the ticks of 24 hours ago?

DateTime has a constructor which takes Ticks as parameter. Extract ticks from file name, parse them to long and create a DateTime type object. Later you can select records older than a day.

DateTime dt = new DateTime(633614215998593750);

To select dates older than a day use:

if(dt <= DateTime.Now.AddDays(-1))

TimeSpan.TicksPerDay将为您提供每天的滴答声,因此只需从当前滴答声中减去滴答声即可获得24小时前的滴答声,例如:

var ticks24HoursAgo = DateTime.UtcNow.Ticks - TimeSpan.TicksPerDay;

This should do :

DateTime dt = new DateTime(ticks);

if (DateTime.Now - dt > TimeSpan.FromDays(1))
    {
       //do something
    }

Also - you can work with UTC instead of local

每个DateTime对象都有一个Ticks属性,因此请在24小时前获取(将-1天添加到DateTime.Now应该足够了),然后删除所有小于计算时间的滴答声的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM