Tech Off Post

Single Post Permalink

View Thread: C# Sting converstion
  • User profile image
    W3bbo

    TommyCarlier said:
    W3bbo said:
    *snip*

    Converting to string, substringing and parsing to integers is not really nice. And the DateTime-constructor you use is used wrongly: the first argument is the year and not the day.

    Long live integer division and modulo:

    int d = 25122009;
    DateTime date =
      new DateTime(
        d / 1000000,
        (d / 10000) % 100,
        d % 10000);

    Indeed, string conversion is hackish, but it has the benefit of being human-readable without needing to think too much about base-10 division.

     

    As for why I got the parameters wrong: I was getting it right in principle, I wasn't intending for my code to be taken verbatim (and if they did, it's a good exercise in why it's a good idea not to, so I can argue I got it wrong intentionally Big Smile ).