14位时间的转化

   using System;
using System.Collections.Generic;

public class MyClass
{
 public static void RunSnippet()
 {
    string a=DateTime.Now.ToString("yyyyMMddhhmmss");
 DateTime dt = DateTime.Parse(string.Format( new MyDateTimeFormat(), "{0:yyyyMMddhhmmss}", a));
 string b=dt.ToString("yyyyMMddhhmmss");
  
  WL("转化前的时间   "+a);
  WL(string.Format("{0:yyyyMMddhhmmss}", a));
  WL(string.Format( new MyDateTimeFormat(), "{0:yyyyMMddhhmmss}", a));
  WL("时间格式   "+dt.ToString());
  WL("转化后的时间   "+b);
 }
 
 #region Helper methods
 
 public static void Main()
 {
  try
  {
   RunSnippet();
  }
  catch (Exception e)
  {
   string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
   Console.WriteLine(error);
  }
  finally
  {
   Console.Write("Press any key to continue...");
   Console.ReadKey();
  }
 }

 private static void WL(object text, params object[] args)
 {
  Console.WriteLine(text.ToString(), args); 
 }
 
 private static void RL()
 {
  Console.ReadLine(); 
 }
 
 private static void Break()
 {
  System.Diagnostics.Debugger.Break();
 }

 #endregion
 
 //内部类
 private class MyDateTimeFormat : IFormatProvider, ICustomFormatter
{

    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
  {
            return this;
  }
        else
  {
     return null;
  }
    }
    public string Format(string format, object arg, IFormatProvider provider)
    {
        string inputDate = Convert.ToString(arg);
        return string.Format("{0}/{1}/{2} {3}:{4}:{5}",
            inputDate.Substring(4, 2),
            inputDate.Substring(6, 2),
            inputDate.Substring(0, 4),
            inputDate.Substring(8, 2),
            inputDate.Substring(10, 2),
            inputDate.Substring(12, 2));
    }
}
}

posted on 2010-06-18 16:53  xrt2004  阅读(382)  评论(0编辑  收藏  举报

导航