c#時間戳相互轉換


  /// <summary>
    /// 獲取時間戳
    /// </summary>
    /// <returns></returns>
    public static string GetTimeSpan(System.DateTime time)
    {
        long ts = GetUnixTime(time);
        return ts.ToString();
    }

    /// <summary>  
    /// 將DateTime時間格式轉換為Unix時間戳格式  
    /// </summary>
    /// <param name="time">時間</param>  
    /// <returns>long</returns>  
    public static long GetUnixTime(System.DateTime time)
    {
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
        long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000調整為13位      
        return t;
    }
    /// <summary>        
    /// 將Unix時間戳轉為C#時間格式      
    /// </summary>
    /// <param name="timeSpan"></param>        
    /// <returns></returns>        
    public static DateTime GetDateTimeFromUnix(string timeSpan)
    {
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeSpan + "0000");
        TimeSpan toNow = new TimeSpan(lTime);
        return dtStart.Add(toNow);
    }

/// <summary>
/// 創建時間
/// </summary>
public string createTime { get; set; }

public DateTime _createTime {
get
{
if (string.IsNullOrEmpty(this.createTime))
return DateTime.MinValue;
return TimerTool.GetDate

 /// <summary>
        /// 根據字節長度來截取字符串
        /// </summary>
        ///<param name="origStr">原始字符串</param>
        ///<param name="length">提取前length個字節</param>
        /// <returns></returns>
        public static String CutByteString(string origStr, int length)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(origStr);
            int n = 0; //  表示當前的字節數
            int i = 0; //  要截取的字節數
            for (; i < bytes.GetLength(0) && n < length; i++)
            {
                //  偶數位置,如0、2、4等,為UCS2編碼中兩個字節的第一個字節
                if (i % 2 == 0)
                {
                    n++; //  在UCS2第一個字節時n加1
                }
                else
                {
                    //  當UCS2編碼的第二個字節大於0時,該UCS2字符為漢字,一個漢字算兩個字節
                    if (bytes[i] > 0)
                    {
                        n++;
                    }
                }
            }
            //  如果i為奇數時,處理成偶數
            if (i % 2 == 1)
            {
                //  該UCS2字符是漢字時,去掉這個截一半的漢字

                if (bytes[i] > 0)
                    i = i - 1;

                //  該UCS2字符是字母或數字,則保留該字符
                else
                    i = i + 1;
            }
            return Encoding.Unicode.GetString(bytes, 0, i);
        }

   var countlen = System.Text.Encoding.Default.GetByteCount(LocalDataManager.Instance.UserModel.student.name);
                NameText.text = countlen > 12 ? CutByteString(LocalDataManager.Instance.UserModel.student.name, 12) : LocalDataManager.Instance.UserModel.student.name;

 

TimeFromUnix(this.createTime);
}
}


免責聲明!

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



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