Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Replacing all instances of UTC time in a string

I'm in the progress of upgrading a web application from .NET 1.1 to 2.0 and came across this function that I created which replaces all instances of date/time in a string. In the application users add notes and when it gets saved to the database it appends Added By [User] on [Date/Time] to the end of the notes. The date/time is added as UTC format, so I had to come up with a way to replace each instance of date/time to the users local time when the notes are viewed. Here's how I did it:

using System;

using System.Text.RegularExpressions;

 

public class TimeZoneConversionHelper

{

   public static string RegexReplaceTime( string input, double timeZone)

   {

      string convertedText = String .Empty;

      if ((input != String .Empty) && (input != null ))

      {

        string pattern = @"\?*(((((0[13578])|([13578])|(1[02]))[\-\/\s]?\b((0[1-9])|([1-9])|([1-2][0-

9])|(3[01])))|((([469])|(11))[\-\/\s]?\b((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-\/\s]?\b((0[1-

9])|([1-9])|([1-2][0-9]))))[\-\/\s]?\b\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5]

[0-9])\s))([AM|PM|am|pm]{2,2})))?" ;

 

        MatchCollection matchCollection = Regex .Matches(input, pattern);

 

        foreach ( Match match in matchCollection)

        {

            DateTime conversionDateTime = TimeZoneConversion( Convert .ToDateTime(match.Value), timeZone);

            convertedText = input.Replace(match.ToString(), conversionDateTime.ToString());

        }

      }

 

      return convertedText;

   }

 

   public static DateTime TimeZoneConversion( DateTime utcTime, double timeZone)

   {

      return utcTime.AddHours(timeZone);

   }

}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Sunday, February 26, 2006 3:38 PMby Ryan Olshan
Filed under:

Comments

No Comments

New Comments to this post are disabled