Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. combining number and dateformating in one funcrtion

combining number and dateformating in one funcrtion

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stephan_007
    wrote on last edited by
    #1

    hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this. public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above } So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan.

    G P 2 Replies Last reply
    0
    • S stephan_007

      hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this. public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above } So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan.

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      stephan_007, Why would you want to do this? there are already methods in place to do this. Regards, Gareth.

      1 Reply Last reply
      0
      • S stephan_007

        hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this. public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above } So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Why take a string as input? Even an object would be better:

        if ( input is int ) ((int) input).ToString ( formatstring ) ;
        else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;

        But would calling the appropriate ToString directly help?

        someint.ToString ( "#,##0.0000" ) ;

        somedate.ToString ( "dd/MM/yyyy" ) ;

        (Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          Why take a string as input? Even an object would be better:

          if ( input is int ) ((int) input).ToString ( formatstring ) ;
          else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;

          But would calling the appropriate ToString directly help?

          someint.ToString ( "#,##0.0000" ) ;

          somedate.ToString ( "dd/MM/yyyy" ) ;

          (Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)

          S Offline
          S Offline
          stephan_007
          wrote on last edited by
          #4

          i parse from a textfile which contains infos like |value|{#,##0.0000} |date|{0:dd/MM/yyyy} when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring. so the input is always a string. i do also provide the culture info which corresponds to the string format so it is formated correctly. input.ToString(FormatString, new CultureInfo(LocalString));

          P 1 Reply Last reply
          0
          • S stephan_007

            i parse from a textfile which contains infos like |value|{#,##0.0000} |date|{0:dd/MM/yyyy} when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring. so the input is always a string. i do also provide the culture info which corresponds to the string format so it is formated correctly. input.ToString(FormatString, new CultureInfo(LocalString));

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Sounds like a bad design; go slap the person responsible. Why doesn't the system that writes the file perform the format?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups