• C# 类--(Date time)


    输入一个日期判断是否正确:


    Console.WriteLine(" 请按照格式正确输入年月日(yyy/MM/dd)");
    string a = Console.ReadLine();
    DateTime dt = new DateTime();
    dt = DateTime.Parse(a);
    string s;
    s = dt.ToString("yyyy年MM月dd日");
    string y = s.Substring(0, 4);
    string m = s.Substring(5, 2);
    string d = s.Substring(8, 2);
    int y1 = Convert.ToInt32(y);
    int m1 = Convert.ToInt32(m);
    int d1 = Convert.ToInt32(d);
    int runnian = 0;
    if (y1 % 4 == 0 && y1 % 100 != 0 || y1 % 400 == 0)
    {
    runnian = 1;
    }
    if (y1 > 1 && y1 <= 9999 && m1 > 0 && m1 <= 12 && d1 > 0 && d1 <= 31)
    {
    if (m1 == 4 || m1 == 6 || m1 == 8 || m1 == 11 && d1 > 30)
    {
    Console.WriteLine("输入错误");
    }
    else if (m1 == 2 && d1 > 29)
    {
    Console.WriteLine("输入错误");
    }
    else if (m1 == 2 && d1 == 29 && runnian != 1)
    {
    Console.WriteLine("输入错误!" + y1 + "不是闰年,2月没有29号!");
    }
    else
    {
    Console.WriteLine(s);
    Console.WriteLine("输入正确");
    }

    }
    Console.ReadLine();

    方法2:


    Console.WriteLine(" 请按照格式正确输入年月日(yyy/MM/dd)");
    string a = Console.ReadLine();
    try
    {
    DateTime dt = new DateTime();
    dt = Convert.ToDateTime(a);
    Console.WriteLine("输入正确");
    }
    catch (Exception)
    {

    Console.WriteLine("输入错误");
    }
    Console.ReadLine();

  • 相关阅读:
    codeforces 1140D(区间dp/思维题)
    区间dp之四边形不等式优化详解及证明
    poj1947
    hdu4044
    poj1155
    poj3162(树形dp+优先队列)
    hdu4003详解(树形dp+多组背包)
    poj1947(树形背包)
    hdu1011(树形背包)(提供一个特殊样例)
    CopyOnWriteArrayList总结
  • 原文地址:https://www.cnblogs.com/likaixuan/p/4333401.html
Copyright © 2020-2023  润新知