Thursday, September 8, 2011

string formatting DateTime

http://www.csharp-examples.net/string-format-datetime/

 
 

Addendum 9/19/2014: This test passes:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TimeKeeping.Tests
{
   [TestClass]
   public class UnitTests
   {
      [TestMethod]
      public void Test()
      {
         DateTime yin = new DateTime(2013,1,2,4,5,6);
         DateTime yang = new DateTime(2013,11,12,14,15,16);
         Assert.AreEqual(String.Format("{0:MM/dd/yyyy HH:mm:ss tt}", yin),
               "01/02/2013 04:05:06 AM");
         Assert.AreEqual(String.Format("{0:MM/dd/yyyy HH:mm:ss tt}", yang),
               "11/12/2013 14:15:16 PM");
         Assert.AreEqual(String.Format("{0:M/d/yy h:m:s tt}", yin), "1/2/13 4:5:6 AM");
         Assert.AreEqual(String.Format("{0:M/d/yy h:m:s tt}", yang), "11/12/13 2:15:16 PM");
      }
   }
}

No comments:

Post a Comment