Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created March 21, 2012 15:41
Show Gist options
  • Save masaru-b-cl/2148687 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/2148687 to your computer and use it in GitHub Desktop.
Cast to Datetime from generic type value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static void Write<T>(T value)
{
if (value is DateTime)
{
var datetime = (DateTime)((object)value);
Console.WriteLine(datetime.ToString("yyyyMMddHHmmss"));
}
else
{
Console.WriteLine(value);
}
}
static void Main(string[] args)
{
Write(DateTime.Now);
Write(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment