zoukankan      html  css  js  c++  java
  • 【Unity/C#】DateTime时间字符串,月份用英文显示

    制作一个钟表,要求效果如下图:
    这里写图片描述

    由于每一部分的字体大小不同,我分别使用了不同的Text控件。(不懂dalao们有没有更科学的办法)

    这里写图片描述

    把这些Text控件包含在一个Object下,给该Object定义一个脚本,分别引用这些控件。

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    /// <summary>
    /// 显示当前的系统时间
    /// </summary>
    public class TimeScript : MonoBehaviour {
    
        public Text HourText;   // 时
        public Text MinuteText; // 分
        public Text DateText;   // 23
        public Text MonthText;  // MAR
        public Text WeekText;   // THU
    
        // Use this for initialization
        void Start () {
    
        }
    
        // Update is called once per frame
        void Update () {
            //取得现在的时间
            DateTime now = DateTime.Now;
    
            HourText.text = now.ToString("HH");  // HH是24时制,hh是12时制
            MinuteText.text = now.ToString("mm");
            DateText.text = now.ToString("dd");
            MonthText.text = now.ToString("MMMM", new System.Globalization.CultureInfo("en-us")).Substring(0, 3); // 月份只要洋文的前三个字母
            WeekText.text = now.ToString("dddd").Substring(0, 3);   // 星期只要洋文的前三个字母
    
        }
    }
    

    小结:

    • 时间字符串使用DateTime类。
    • 格式化的格式参见 http://www.cnblogs.com/polk6/p/5465088.html
    • 月份获取到的是数字(如12月是返回12),想要改成英文,国际化的方法参见上面代码(省去自己写12个枚举)。

    其他字符串操作的相关参考:

    http://www.manew.com/blog-1121-845.html

  • 相关阅读:
    pyQt5新手教程 (二)开始你的旅程-编写
    未来写作
    电脑使用问题
    cad学习问题
    chm制作
    github学习之路
    python解决实际问题
    英语学习
    经济学
    通过playgrounds程序学习swift语言及思想
  • 原文地址:https://www.cnblogs.com/guxin/p/csharp-unity-datetime-format.html
Copyright © 2011-2022 走看看