2021年12月8日 星期三

C# 抓取指定月份顯示民國 和 Day與Week

 利用 C# 抓取年月與週,以下程式設計方式: Interface方式來呈現

Calendar (C#) 腳本----參數管理----:  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Calendar 
{
    private string year;    // 年
    private string month;   // 月
    private string days;    // 日

    GetCalenderData getCalenderData;

    public Calendar(string year, string month, string day, GetCalenderData getCalenderData)
    {
        this.year = year;
        this.month = month;
        this.days = day;
        this.getCalenderData = getCalenderData;
    }

    public List<Data_data> getData()
    {
        return getCalenderData.getData(int.Parse(year), int.Parse(month), int.Parse(days));
    }

    public int dayNumber()
    {
        return getCalenderData.dayNumber(int.Parse(year), int.Parse(month), int.Parse(days));
    }
}

public interface GetCalenderData
{
    List<Data_data> getData(int year, int month, int day);     // 取得行事曆資料
    int dayNumber(int year, int month, int day);
}

// 暫存用
public class Data_data
{
    public string year;
    public string month;
    public string day;
    public string week;

    public Data_data(string year, string month, string day, string week)
    {
        this.year = year;
        this.month = month;
        this.day = day;
        this.week = week;
    }
}
InterfaceCalender (C#) 腳本----方法撰寫---- :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class InterfaceCalender : GetCalenderData
{
    DateTime dateTime;

    public int dayNumber(int year, int month, int day)
    {
        int dayNum = 0;

        for (int x = 1; x <= DateTime.DaysInMonth(dateTime.Year, dateTime.Month); x++)
        {
            dayNum = x;
        }
    
        return dayNum;
    }

    // 取得行事曆資料
    public List<Data_data> getData(int year, int month, int day)
    {
        List<Data_data> classExcels = new List<Data_data>();

        for(int i = 1; i <= DateTime.DaysInMonth(dateTime.Year, dateTime.Month); i++)
        {
            dateTime = new DateTime(year, month, i);
            
            classExcels.Add(new Data_data(yearConversion(dateTime.Year).ToString(), dateTime.Month.ToString(), i.ToString(),
                weekEngToChin(dateTime.DayOfWeek.ToString())));
    }

        return classExcels;
    }

    // 西元轉民國(公式)
    int yearConversion(int inVid)
    {
        return inVid - 1911;
    }

    // 英文轉中文(星期)
    string weekEngToChin(string dayOfWeek)
    {
        string chinWeek = "";

        if (dayOfWeek.ToString().Equals("Monday"))
            chinWeek = "一";
        if (dayOfWeek.ToString().Equals("Tuesday"))
            chinWeek = "二";
        if (dayOfWeek.ToString().Equals("Wednesday"))
            chinWeek = "三";
        if (dayOfWeek.ToString().Equals("Thursday"))
            chinWeek = "四";
        if (dayOfWeek.ToString().Equals("Friday"))
            chinWeek = "五";
        if (dayOfWeek.ToString().Equals("Saturday"))
            chinWeek = "六";
        if (dayOfWeek.ToString().Equals("Sunday"))
            chinWeek = "日";

        return chinWeek;
    }
}
TestRun (C#) 腳本----執行----:

public class TestRun : MonoBehaviour{ Calendar calendar; void Start() { calendar = new Calendar("2021", "12", "1", new InterfaceCalender()); } void Update() { foreach(var str in calendar.getData()) { Debug.Log(str.month +"月" + str.day + "日" + "星期:" + str.week); } } }

結果圖:


沒有留言:

張貼留言