Daniel De junior
Daniel De junior

Reputation: 119

How can i create this format of date time directory but with my format?

The test i did is:

string date = DateTime.Now.ToString("ddd dd.MM.yyyy");
string time = DateTime.Now.ToString("HH.mm tt");
string format = "{0} from {1} At {2}";
string cp = string.Format(format, "", date, time);
Directory.CreateDirectory(@"c:\\temp\\" + cp);

The result in the variable cp is: from Fri 20.01.2017 At 09.27 AM And there is no problem to create this directory.

This is my code:

for (int i = 0; i < countriesNames.Count(); i++)
            {
                string pathDateTime = urls[0].Substring(48, 12);
                string pathDateTimeLast = urls[urls.Count - 1].Substring(48, 12);
                var d = DateTime.ParseExact(pathDateTime, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
                var e = DateTime.ParseExact(pathDateTimeLast, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
                string country = countriesNames[i].Substring(15);
                string f = "{0} from {1} At {2} until {3}";
                string countryPath = countriesMainPath + "\\" + country + "\\" + string.Format(f, "", d,e);
                if (!Directory.Exists(countryPath))
                {
                    Directory.CreateDirectory(countryPath);
                }
                countryPaths.Add(countryPath);
            }

The way i did it with the 'f' variable is not right and not working fine give me exception.

In my code in the variable 'd' there is 20/01/2017 05:15:00 And in variable 'e' 20/01/2017 07:30:00

But i can't create this directories. So i want to format my date and time after extracting them to be like the format in the first example: from Fri 20.01.2017 At 09.27 AM but with my date and time.

For example my directory should be something like:

from Fri 20.1.2017 At 05:15 AM Until 20.1.2017 At 07:30 AM Then to create this directory: "from Fri 20.1.2017 At 05:15 AM Until 20.1.2017 At 07:30 AM"

The question is how do i format my dates and times after parsed to this format ?

Upvotes: 0

Views: 1067

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131749

You are trying to create a path by formatting dates using your current locale's default (long) format. In most countries the date separator is / and the time separator is always :. This results in invalid paths.

It's a bit hard to understand what format you want to use, since you mix calls to String.Format and concatenate the results. It seems that the original path should be:

var cp=String.Format(@"c:\temp\From {0:ddd dd.MM.yyyy} At {0:HH.mm tt}",DateTime.Now);

or

var root="c:\temp\";
var partialPath = String.Format("From {0:ddd dd.MM.yyyy} At {0:HH.mm tt}",DateTime.Now)
var cp=Path.Combine(root,partialPath);

You don't need to format each component separately. If you check the documentation of String.Format you'll see that you can use a composite format string for each placeholder.

The country path seems to be

var partialPath = String.Format(@"{0}\from {1:ddd dd.MM.yyyy} At {1:HH.mm tt} until {2:HH.mm tt}",
                                country,d,e);
var countryPath =Path.Combine(countriesMainPath,partialPath);

That said, I wouldn't use that date format. The resulting folder names can't be sorted in a meaningful way making it difficult for users to find folders by date. I'd use the yyyy-MM-dd format, or yyyy-MM-dd ddd if the name of the day is really necessary.

Upvotes: 2

Related Questions