landv / landv.github.io

landv-blogs
https://landv.cn
2 stars 0 forks source link

C#时间步进循环 #89

Open landv opened 4 weeks ago

landv commented 4 weeks ago
class Program
{
    static void Main()
    {
        DateTime start = new DateTime(2024, 1, 1, 0, 0, 0); // 起始时间
        DateTime end = new DateTime(2024, 1, 20, 22, 0, 0); // 结束时间
        TimeSpan interval = TimeSpan.FromHours(2); // 步进值

        for (DateTime current = start; current <= end; current = current.Add(interval))
        {
            //            Console.WriteLine(current); // 打印当前时间
            int year = current.Year;
            int month = current.Month;
            int day = current.Day;
            int hour = current.Hour;

            //Console.WriteLine($"年: {year}, 月: {month}, 日: {day}, 时: {hour}");
            // 输出时间点和 "男"
            Console.WriteLine($"年: {year}, 月: {month}, 日: {day}, 时: {hour}, 性别: 男");

            // 输出时间点和 "女"
            Console.WriteLine($"年: {year}, 月: {month}, 日: {day}, 时: {hour}, 性别: 女");
        }
    }
}