Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Snake / Nibbles clone in C and Ncurses
Please improve
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Simple Particle Engine for a shooter game
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
Parse Relative Date
<pre class='prettyprint' language='cs'>class RelativeDateParser { public static DateTime Parse(string input) { DateTime dt = DateTime.Now; // parse "x days x hours x minutes x seconds" or "x days x hours x minutes x seconds ago" Regex r = new Regex("(day)|(hour)|(minues)|(second)"); if (r.Match(input).Success == true) { dt = ParseDHMS(input); } // parse "yesterday" or "today" or "tomorrow" or "eow" or "eod" r = new Regex("(today)|(tomorrow)|(eow)|(eod)"); if (r.Match(input).Success == true) { dt = ParseGenericRelative(input); } Console.WriteLine("Now DateTime: " + DateTime.Now.ToString()); Console.WriteLine("New DateTime: " + dt.ToString()); return dt; } private static DateTime ParseGenericRelative(string input) { throw new NotImplementedException("Not implemented"); } private static DateTime ParseDHMS(string input) { DateTime dt; double days = 0; double hours = 0; double minutes = 0; double seconds = 0; // search for days Regex r = new Regex(@"(\d+)\s*day"); Match m = r.Match(input); if (m.Success == true) { string match = m.Groups[1].Value; days += Double.Parse(match); } // search for hours r = new Regex(@"(\d+)\s*hour"); m = r.Match(input); if (m.Success == true) { string match = m.Groups[1].Value; hours += Double.Parse(match); } // search for minutes r = new Regex(@"(\d+)\s*minute"); m = r.Match(input); if (m.Success == true) { string match = m.Groups[1].Value; minutes += Double.Parse(match); } // search for seconds r = new Regex(@"(\d+)\s*second"); m = r.Match(input); if (m.Success == true) { string match = m.Groups[1].Value; seconds += Double.Parse(match); } // In past? r = new Regex(@"ago"); m = r.Match(input); if (m.Success == true) { days = days * -1; hours = hours * -1; minutes = minutes * -1; seconds = seconds * -1; } ///////////////////// // Add offsets to now dt = DateTime.Now.AddDays(days).AddHours(hours).AddMinutes(minutes).AddSeconds(seconds); return dt; } }</pre> <a href="http://www.refactormycode.com/codes/488-parse-relative-date" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>