Parse DateTime from "2011/5/2 14:40"
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Drawing;
public static class Utility
{
/// <summary>
/// Parse DateTime from "2011/5/2 14:40"
/// </summary>
/// <param name="s">"2011/5/2 14:40"</param>
/// <returns>Parsed DateTime if success, DateTime.Now if not success</returns>
public static DateTime ParseDateTime(string s)
{
DateTime result = DateTime.Now;
DateTime r;
try
{
var datetime = s.Split(' ');
var date = datetime[0].Split('/');
var time = datetime[1].Split(':');
int year = int.Parse(date[0]);
int month = int.Parse(date[1]);
int day = int.Parse(date[2]);
int hour = int.Parse(time[0]);
int minute = int.Parse(time[1]);
result = new DateTime(year, month, day, hour, minute, 0);
}
catch { }
return result;
}
}
Related examples in the same category