简体   繁体   English

如何使用日期格式创建这种格式的日期时间目录?

[英]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. 变量cp中的结果为:from Fri 20.01.2017 At 09.27 AM并且创建此目录没有问题。

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. 我用'f'变量做的方式不正确,不能正常工作给我例外。

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 在我的代码中,变量'd'有20/01/2017 05:15:00变量'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. 所以我想在提取日期和时间后将其格式化为第一个示例中的格式:从Fri 20.01.2017在09.27 AM,但有我的日期和时间。

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" 从2017年1月20日星期五05:15 AM到20.1.2017年07:30 AM然后创建此目录:“从2017年1月20日5:15 AM到2017年1月20日7:30 AM”

The question is how do i format my dates and times after parsed to this format ? 问题是解析后如何格式化日期和时间?

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. 因为混合了对String.Format的调用连接结果,所以很难理解要使用的格式。 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. 如果查看String.Format的文档,您会发现可以为每个占位符使用复合格式字符串。

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. 如果确实需要一天的名称,则使用yyyy-MM-dd格式,或yyyy-MM-dd ddd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM