【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

posted @ 2016-12-19 22:06  霍莉雪特  阅读(2169)  评论(0编辑  收藏  举报