DateTimeHelper


namespace ConsoleApplication
{
    using System;
    using Microshaoft;
    public class Class1
    {
        static void Main(string[] args)
        {
            DateTime x = DateTime.Now;
            x = new DateTime(2008,1,1,13,1,1,1);
            string s = DateTimeHelper.Get_yyyyMMddHHmmss_String(x);
            Console.WriteLine(s);
            Console.WriteLine(DateTimeHelper.Parse_yyyyMMddHHmmss(s));
            Console.WriteLine(DateTimeHelper.Parse_MMddHHmmss(2008, s.Substring(4, 10)));
            x = DateTime.Now.AddSeconds(5);
            Console.WriteLine(DateTimeHelper.Get_yyyyMMddHHmmss_String(x));
            while ("q" != Console.ReadLine())
            {
                Console.WriteLine(DateTimeHelper.SecondsDiffNow(x));
                DateTime dt = DateTime.Now;
                Console.WriteLine(DateTimeHelper.GetAlignSecondsDateTime(dt, 7 * 60));
            }
            Console.WriteLine(Environment.Version.ToString());
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Globalization;
    public static class DateTimeHelper
    {
        public static bool IsVaildateTimestamp(DateTime timeStamp, int timeoutSeconds)
        {
            long l = SecondsDiffNow(timeStamp);
            return ((l > 0) && (l < timeoutSeconds));
        }
        public static long MillisecondsDiffNow(DateTime time)
        {
            long now = DateTime.Now.Ticks;
            long t = time.Ticks;
            return (t - now)/10000;
        }
        public static long SecondsDiffNow(DateTime time)
        {
            return MillisecondsDiffNow(time)/1000;
        }
        public static DateTime GetAlignSecondsDateTime(DateTime time, long alignSeconds)
        { 
            long ticks = time.Ticks;
            ticks -= ticks % (10000 * 1000 * alignSeconds);
            DateTime dt = new DateTime(ticks);
            return dt ;
        }
        public static string Get_MMddHHmmss_String(DateTime time)
        {
            return time.ToString("MMddHHmmss");
        }
        public static string Get_yyyyMMddHHmmss_String(DateTime time)
        {
            return time.ToString("yyyyMMddHHmmss");
        }
        public static string Get_yyyyMMdd_String(DateTime time)
        {
            return time.ToString("yyyyMMdd");
        }
        public static DateTime Parse_yyyyMMddHHmmss(string text)
        {
            DateTime time = DateTime.TryParseExact
                                (
                                    text
                                    , "yyyyMMddHHmmss"
                                    , DateTimeFormatInfo.InvariantInfo
                                    , DateTimeStyles.None
                                    , out time
                                ) ? time : DateTime.MinValue;
            return time;
        }
        public static DateTime Parse_MMddHHmmss(int year, string text)
        {
            return Parse_yyyyMMddHHmmss(year.ToString("0000") + text);
        }
    }
}

posted @ 2008-10-29 00:28  于斯人也  阅读(6770)  评论(1编辑  收藏  举报