c# - How to parse timestamp either as NTP timestamp or units? -


i need parse timestamp value can either given ntp time or short time string unit characters.

examples:

time = 604800 (can cast long, easy!) 

or

time = 7d 

is there built-in date time parsing functionality in .net such cases? or have character not numeric (probably regex?).

the following characters expected occur:

  d - days    h - hours    m - minutes    s - seconds 

no regex needed such basic operation.

public static int process(string input) {     input = input.trim();                                          // removes leading , trailing white-space characters       char lastchar = input[input.length - 1];                       // gets last character of input      if (char.isdigit(lastchar))                                    // if last character digit         return int.parse(input, cultureinfo.invariantculture);     // returns converted input, using independent culture (easy ;)      int number = int.parse(input.substring(0, input.length - 1),   // gets number represented input (except last character)                            cultureinfo.invariantculture);          // using independent culture      switch (lastchar)     {         case 's':             return number;         case 'm':             return number * 60;         case 'h':             return number * 60 * 60;         case 'd':             return number * 24 * 60 * 60;         default:             throw new argumentexception("invalid argument format.");     } } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -