Use date as a filepath












0















I need the following filepath according to date:




yyyyMMd




and I though I could it in one call to DateTime class like this:



string filepath = DateTime.Now.ToString(@"yyyyMMd");


However, it yields:
"2018M11d" which is wrong.



Is there a way to escape the backwards slahes?










share|improve this question




















  • 1





    I think the filesystem can't use as part of the filename

    – Oddmar Dam
    Nov 22 '18 at 9:49






  • 2





    Isn't a backslash a forbidden file name character? (Example fiddle)

    – Uwe Keim
    Nov 22 '18 at 9:49













  • It is, rather replace the blackslashes with dots, it would still make sense as a data

    – mahlatse
    Nov 22 '18 at 9:50











  • Do you want this output 20181122

    – CodeConstruct
    Nov 22 '18 at 10:03
















0















I need the following filepath according to date:




yyyyMMd




and I though I could it in one call to DateTime class like this:



string filepath = DateTime.Now.ToString(@"yyyyMMd");


However, it yields:
"2018M11d" which is wrong.



Is there a way to escape the backwards slahes?










share|improve this question




















  • 1





    I think the filesystem can't use as part of the filename

    – Oddmar Dam
    Nov 22 '18 at 9:49






  • 2





    Isn't a backslash a forbidden file name character? (Example fiddle)

    – Uwe Keim
    Nov 22 '18 at 9:49













  • It is, rather replace the blackslashes with dots, it would still make sense as a data

    – mahlatse
    Nov 22 '18 at 9:50











  • Do you want this output 20181122

    – CodeConstruct
    Nov 22 '18 at 10:03














0












0








0








I need the following filepath according to date:




yyyyMMd




and I though I could it in one call to DateTime class like this:



string filepath = DateTime.Now.ToString(@"yyyyMMd");


However, it yields:
"2018M11d" which is wrong.



Is there a way to escape the backwards slahes?










share|improve this question
















I need the following filepath according to date:




yyyyMMd




and I though I could it in one call to DateTime class like this:



string filepath = DateTime.Now.ToString(@"yyyyMMd");


However, it yields:
"2018M11d" which is wrong.



Is there a way to escape the backwards slahes?







c# .net datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:06









Konamiman

42.6k1597126




42.6k1597126










asked Nov 22 '18 at 9:47









kalle31kalle31

6




6








  • 1





    I think the filesystem can't use as part of the filename

    – Oddmar Dam
    Nov 22 '18 at 9:49






  • 2





    Isn't a backslash a forbidden file name character? (Example fiddle)

    – Uwe Keim
    Nov 22 '18 at 9:49













  • It is, rather replace the blackslashes with dots, it would still make sense as a data

    – mahlatse
    Nov 22 '18 at 9:50











  • Do you want this output 20181122

    – CodeConstruct
    Nov 22 '18 at 10:03














  • 1





    I think the filesystem can't use as part of the filename

    – Oddmar Dam
    Nov 22 '18 at 9:49






  • 2





    Isn't a backslash a forbidden file name character? (Example fiddle)

    – Uwe Keim
    Nov 22 '18 at 9:49













  • It is, rather replace the blackslashes with dots, it would still make sense as a data

    – mahlatse
    Nov 22 '18 at 9:50











  • Do you want this output 20181122

    – CodeConstruct
    Nov 22 '18 at 10:03








1




1





I think the filesystem can't use as part of the filename

– Oddmar Dam
Nov 22 '18 at 9:49





I think the filesystem can't use as part of the filename

– Oddmar Dam
Nov 22 '18 at 9:49




2




2





Isn't a backslash a forbidden file name character? (Example fiddle)

– Uwe Keim
Nov 22 '18 at 9:49







Isn't a backslash a forbidden file name character? (Example fiddle)

– Uwe Keim
Nov 22 '18 at 9:49















It is, rather replace the blackslashes with dots, it would still make sense as a data

– mahlatse
Nov 22 '18 at 9:50





It is, rather replace the blackslashes with dots, it would still make sense as a data

– mahlatse
Nov 22 '18 at 9:50













Do you want this output 20181122

– CodeConstruct
Nov 22 '18 at 10:03





Do you want this output 20181122

– CodeConstruct
Nov 22 '18 at 10:03












3 Answers
3






active

oldest

votes


















4














Yes. You can.



string filepath = DateTime.Now.ToString(@"yyyy\MM\d");





share|improve this answer
























  • No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

    – mjwills
    Nov 22 '18 at 9:51








  • 2





    Please try it yourself: dotnetfiddle.net/ogNQgn

    – mb14
    Nov 22 '18 at 9:53






  • 2





    In a file name yes. Not in a file path @UweKeim.

    – mjwills
    Nov 22 '18 at 9:54





















4














The safest way is to split the date in its individual components and then use Path.Combine:



var dateParts = DateTime.Now.ToString("yyyy MM d").Split(' ');
var filePath = Path.Combine(dateParts);


This isolates you from the underlying filesystem details you aren't really concerned about (i.e. which character is used as the directory separator).






share|improve this answer































    -1














    You can do this:



    DateTime.Now.ToShortDateString();


    This will help you.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428053%2fuse-date-as-a-filepath%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Yes. You can.



      string filepath = DateTime.Now.ToString(@"yyyy\MM\d");





      share|improve this answer
























      • No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

        – mjwills
        Nov 22 '18 at 9:51








      • 2





        Please try it yourself: dotnetfiddle.net/ogNQgn

        – mb14
        Nov 22 '18 at 9:53






      • 2





        In a file name yes. Not in a file path @UweKeim.

        – mjwills
        Nov 22 '18 at 9:54


















      4














      Yes. You can.



      string filepath = DateTime.Now.ToString(@"yyyy\MM\d");





      share|improve this answer
























      • No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

        – mjwills
        Nov 22 '18 at 9:51








      • 2





        Please try it yourself: dotnetfiddle.net/ogNQgn

        – mb14
        Nov 22 '18 at 9:53






      • 2





        In a file name yes. Not in a file path @UweKeim.

        – mjwills
        Nov 22 '18 at 9:54
















      4












      4








      4







      Yes. You can.



      string filepath = DateTime.Now.ToString(@"yyyy\MM\d");





      share|improve this answer













      Yes. You can.



      string filepath = DateTime.Now.ToString(@"yyyy\MM\d");






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 22 '18 at 9:50









      mb14mb14

      1086




      1086













      • No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

        – mjwills
        Nov 22 '18 at 9:51








      • 2





        Please try it yourself: dotnetfiddle.net/ogNQgn

        – mb14
        Nov 22 '18 at 9:53






      • 2





        In a file name yes. Not in a file path @UweKeim.

        – mjwills
        Nov 22 '18 at 9:54





















      • No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

        – mjwills
        Nov 22 '18 at 9:51








      • 2





        Please try it yourself: dotnetfiddle.net/ogNQgn

        – mb14
        Nov 22 '18 at 9:53






      • 2





        In a file name yes. Not in a file path @UweKeim.

        – mjwills
        Nov 22 '18 at 9:54



















      No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

      – mjwills
      Nov 22 '18 at 9:51







      No it doesn't @kalle31. stackoverflow.com/questions/5465923/…

      – mjwills
      Nov 22 '18 at 9:51






      2




      2





      Please try it yourself: dotnetfiddle.net/ogNQgn

      – mb14
      Nov 22 '18 at 9:53





      Please try it yourself: dotnetfiddle.net/ogNQgn

      – mb14
      Nov 22 '18 at 9:53




      2




      2





      In a file name yes. Not in a file path @UweKeim.

      – mjwills
      Nov 22 '18 at 9:54







      In a file name yes. Not in a file path @UweKeim.

      – mjwills
      Nov 22 '18 at 9:54















      4














      The safest way is to split the date in its individual components and then use Path.Combine:



      var dateParts = DateTime.Now.ToString("yyyy MM d").Split(' ');
      var filePath = Path.Combine(dateParts);


      This isolates you from the underlying filesystem details you aren't really concerned about (i.e. which character is used as the directory separator).






      share|improve this answer




























        4














        The safest way is to split the date in its individual components and then use Path.Combine:



        var dateParts = DateTime.Now.ToString("yyyy MM d").Split(' ');
        var filePath = Path.Combine(dateParts);


        This isolates you from the underlying filesystem details you aren't really concerned about (i.e. which character is used as the directory separator).






        share|improve this answer


























          4












          4








          4







          The safest way is to split the date in its individual components and then use Path.Combine:



          var dateParts = DateTime.Now.ToString("yyyy MM d").Split(' ');
          var filePath = Path.Combine(dateParts);


          This isolates you from the underlying filesystem details you aren't really concerned about (i.e. which character is used as the directory separator).






          share|improve this answer













          The safest way is to split the date in its individual components and then use Path.Combine:



          var dateParts = DateTime.Now.ToString("yyyy MM d").Split(' ');
          var filePath = Path.Combine(dateParts);


          This isolates you from the underlying filesystem details you aren't really concerned about (i.e. which character is used as the directory separator).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 10:09









          KonamimanKonamiman

          42.6k1597126




          42.6k1597126























              -1














              You can do this:



              DateTime.Now.ToShortDateString();


              This will help you.






              share|improve this answer






























                -1














                You can do this:



                DateTime.Now.ToShortDateString();


                This will help you.






                share|improve this answer




























                  -1












                  -1








                  -1







                  You can do this:



                  DateTime.Now.ToShortDateString();


                  This will help you.






                  share|improve this answer















                  You can do this:



                  DateTime.Now.ToShortDateString();


                  This will help you.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 10:18









                  Uwe Keim

                  27.5k31130210




                  27.5k31130210










                  answered Nov 22 '18 at 10:13









                  Pratik RathiPratik Rathi

                  66




                  66






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428053%2fuse-date-as-a-filepath%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen