如何將日期中年月日拆分


從數據庫中讀取日期,有時數據庫中存放的日期格式不一致,所以可以把日期格式先統一一下,然后根據需要讀取年月日,代碼如下:

//首先從數據庫中讀取onlinetime數據
string onlinetime = ds.Tables[0].Rows[i]["onlinetime"].ToString();

//將數據庫中的日期轉換成日期形式,再轉換成字符串
onlinetime = Convert.ToDateTime(onlinetime).ToString("yyyy年MM月dd HH:mm:ss");

//截取 月 和 日 字符串
string month = onlinetime.Substring(5, 2);
string day = onlinetime.Substring(8, 2);
//調用下面的getMonthEng()方法 month = getMonthEng(int.Parse(month));

把月份轉化為英文,代碼如下:

private string getMonthEng( int month) 
    {
        string str = "";

        switch (month)
        {
            case 1:
                str = "Jan";
                break;
            case 2:
                str = "Feb";
                break;
            case 3:
                str = "Mat";
                break;
            case 4:
                str = "Apr";
                break;
            case 5:
                str = "May";
                break;
            case 6:
                str = "Jun";
                break;
            case 7:
                str = "Jul";
                break;
            case 8:
                str = "Aug";
                break;
            case 9:
                str = "Seb";
                break;
            case 0:
                str = "Oct";
                break;
            case 11:
                str = "Nov";
                break;
            case 12:
                str = "Dec";
                break;
            default:
                str = "Err";
                break;
        }
        return str;

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM