c# - Zero-pad a date string without converting to a different type -


i have several dates in string form. want ensure have format 09/08/2012 if come in 9/8/2012.

i know convert them datetime objects, strings, , format them way. question comes curiosity, not necessity.
want know if there's way without data transformations. guess if it's possible use string.format() method.

is possible apply zero-padded date format string without changing type part of process?

this easy do.

  1. split string on /.
  2. left-pad each section 2 digits, prepending '0' needed.
  3. join string parts together

code:

var x = "9/7/2017"; var parts = x     .split('/')                         //  <-- step 1     .select(y => y.padleft(2, '0'));    //  <-- step 2 var z = string.join("/", parts);        //  <-- step 3  debug.writeline(z);   // prints 09/07/2017 

as can see code just complicated enough take minute read , understand. it's clearer parse date datetime, reformat desired format.

var x = "9/7/2017"; var z = datetime.parse(x).tostring("mm/dd/yyyy"); 

that's much easier follow.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -