Use date as a filepath
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
add a comment |
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
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
add a comment |
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
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
c# .net datetime
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
Yes. You can.
string filepath = DateTime.Now.ToString(@"yyyy\MM\d");
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
add a comment |
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).
add a comment |
You can do this:
DateTime.Now.ToShortDateString();
This will help you.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Yes. You can.
string filepath = DateTime.Now.ToString(@"yyyy\MM\d");
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
add a comment |
Yes. You can.
string filepath = DateTime.Now.ToString(@"yyyy\MM\d");
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
add a comment |
Yes. You can.
string filepath = DateTime.Now.ToString(@"yyyy\MM\d");
Yes. You can.
string filepath = DateTime.Now.ToString(@"yyyy\MM\d");
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
add a comment |
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
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
answered Nov 22 '18 at 10:09
KonamimanKonamiman
42.6k1597126
42.6k1597126
add a comment |
add a comment |
You can do this:
DateTime.Now.ToShortDateString();
This will help you.
add a comment |
You can do this:
DateTime.Now.ToShortDateString();
This will help you.
add a comment |
You can do this:
DateTime.Now.ToShortDateString();
This will help you.
You can do this:
DateTime.Now.ToShortDateString();
This will help you.
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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