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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Variable for number of days

Variable for number of days

Scheduled Pinned Locked Moved C#
jsonquestion
9 Posts 4 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.
  • M Offline
    M Offline
    Member 14474607
    wrote on last edited by
    #1

    Hello - I am constructing an URL to pass for API call. It has a date value in it.

    DateTime.Today.AddDays(0).ToString("yyyy-MM-dd")

    Is there some way I can pass the (0) dynamically, reading from App.Config file? I tried --

    DateTime.Today.AddDays(ConfigurationManager.AppSettings["CompletedDays"].ToString("yyyy-MM-dd")

    which errored out and did not work. Any way to do this? Thanks!

    L Richard DeemingR 3 Replies Last reply
    0
    • M Member 14474607

      Hello - I am constructing an URL to pass for API call. It has a date value in it.

      DateTime.Today.AddDays(0).ToString("yyyy-MM-dd")

      Is there some way I can pass the (0) dynamically, reading from App.Config file? I tried --

      DateTime.Today.AddDays(ConfigurationManager.AppSettings["CompletedDays"].ToString("yyyy-MM-dd")

      which errored out and did not work. Any way to do this? Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 14474607 wrote:

      which errored out and did not work.

      Sorry, there is no way we can guess what that means. Please edit your question and provide proper details of the problem.

      1 Reply Last reply
      0
      • M Member 14474607

        Hello - I am constructing an URL to pass for API call. It has a date value in it.

        DateTime.Today.AddDays(0).ToString("yyyy-MM-dd")

        Is there some way I can pass the (0) dynamically, reading from App.Config file? I tried --

        DateTime.Today.AddDays(ConfigurationManager.AppSettings["CompletedDays"].ToString("yyyy-MM-dd")

        which errored out and did not work. Any way to do this? Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        var revisedDateTime = DateTime.Now.AddDays( (int) ConfigurationManager.AppSettings["CompletedDays"] );

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        Richard DeemingR 1 Reply Last reply
        0
        • L Lost User

          var revisedDateTime = DateTime.Now.AddDays( (int) ConfigurationManager.AppSettings["CompletedDays"] );

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          The AppSettings indexer returns a string. The code will compile, but will throw an InvalidCastException at runtime.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          L 1 Reply Last reply
          0
          • M Member 14474607

            Hello - I am constructing an URL to pass for API call. It has a date value in it.

            DateTime.Today.AddDays(0).ToString("yyyy-MM-dd")

            Is there some way I can pass the (0) dynamically, reading from App.Config file? I tried --

            DateTime.Today.AddDays(ConfigurationManager.AppSettings["CompletedDays"].ToString("yyyy-MM-dd")

            which errored out and did not work. Any way to do this? Thanks!

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            int completedDays;
            int.TryParse(ConfigurationManager.AppSettings["CompletedDays"], out completedDays);
            return DateTime.Today.AddDays(completedDays).ToString("yyyy-MM-dd");

            Int32.TryParse Method (System) | Microsoft Docs[^] If the setting isn't a valid integer, completedDays will be set to 0. If you want to validate that the setting is valid, check the return value of TryParse.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            pkfoxP 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              The AppSettings indexer returns a string. The code will compile, but will throw an InvalidCastException at runtime.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              50-50 chance. There are some value-object pair settings out there (UWP: ApplicationDataContainer)

              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

              Richard DeemingR 1 Reply Last reply
              0
              • L Lost User

                50-50 chance. There are some value-object pair settings out there (UWP: ApplicationDataContainer)

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                ConfigurationManager.AppSettings almost certainly refers to: ConfigurationManager.AppSettings Property (System.Configuration) | Microsoft Docs[^] That's a NameValueCollection[^], and the indexer[^] returns a string. If there's a different ConfigurationManager class out there with the same name but different semantics, then it was poorly named. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                L 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  ConfigurationManager.AppSettings almost certainly refers to: ConfigurationManager.AppSettings Property (System.Configuration) | Microsoft Docs[^] That's a NameValueCollection[^], and the indexer[^] returns a string. If there's a different ConfigurationManager class out there with the same name but different semantics, then it was poorly named. :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  If you want to get picky, AppSettings is based on a NameObjectCollectionBase. Not my fault it's protected. But I won't whine about it.

                  It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    int completedDays;
                    int.TryParse(ConfigurationManager.AppSettings["CompletedDays"], out completedDays);
                    return DateTime.Today.AddDays(completedDays).ToString("yyyy-MM-dd");

                    Int32.TryParse Method (System) | Microsoft Docs[^] If the setting isn't a valid integer, completedDays will be set to 0. If you want to validate that the setting is valid, check the return value of TryParse.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    pkfoxP Offline
                    pkfoxP Offline
                    pkfox
                    wrote on last edited by
                    #9

                    A much better way of doing things Richard - I often overlook TryParse and default to using one of the ConvertToxxx static methods

                    "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                    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