• DateTime类型ToString时 显示时间 AM/PM


    如果不添加DateTimeFormatInfo.InvariantInfo参数的话,默认是本地化的。

    Console.WriteLine(DateTime.Now.ToString("hh:mm tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)); //12:09 PM
    
    Console.WriteLine(DateTime.Now.ToString("hh:mm tt")); //12:09 下午
    
    Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)); //03/25/2020 12:13:24 PM
    
    DateTime time = DateTime.Now;
    string s = time.ToString("yyyy.MM.dd hh:mm:ss t\M");
    Console.WriteLine(s);
    

    Explanation: t gives the first character of the AM/PM designator (the localized designator can be retrieved in DateTimeFormatInfo.AMDesignator and DateTimeFormatInfo.PMDesignator). The M escapes the M so that DateTime.ToString does not interpret it as part of the format string and print the numerical value of the month.

    Note that once you do this you are explicitly being culture insensitive. For example, in Japan, the AM/PM designator differs in the second characters, not the first.

  • 相关阅读:
    css3skew
    github如何使用
    互联网历史
    html知识点
    人类的终极目标是什么?
    如何提高自我学习能力?
    为什么富人越富,穷人越穷?
    关于游戏小说与学习知识的不同
    关于写代码的一点体会
    监听多行文本框字数输入
  • 原文地址:https://www.cnblogs.com/grj1046/p/12565229.html
Copyright © 2020-2023  润新知