9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C#】現在時刻から直後のきりの良い時刻(5分、10分、15分・・・)を取得する。

Posted at

別のプログラムを書いていて、思いついたのでここに記載。

###解説
現在時刻を秒に直して、300のmodを取ることで、直前のきり良い時刻からの経過時刻を取得。300からそれを引くことで、きりの良い時刻までの時間を取得。現在時刻にそれを加える。

using System;

public class Hello{
  public static void Main(){
    int addSeconds = 300 - (DateTime.Now.Minute*60 + DateTime.Now.Second)%300;
    DateTime d = DateTime.Now.AddSeconds(addSeconds);
    System.Console.WriteLine(d.ToString());
  }
}
9
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?