英文:
How to use custom DateTime string format for time without the date portion?
问题
以下是代码的翻译部分:
protected string PopulateDateInFilename(string filename) {
// 文件名可以包含日期格式模板,将模板替换为实际日期数据
Dictionary<string, string> dateDictionary = new Dictionary<string, string>() {
{"[yyyymmdd]", "yyyyMMdd"},
{"[yyyy-mm-dd]", "yyyy-MM-dd"},
{"[yyyy.mm.dd]", "yyyy.MM.dd"},
{"[mmddyyyy]", "MMddyyyy"},
{"[mm-dd-yyyy]", "MM-dd-yyyy"},
{"[mm.dd.yyyy]", "MM.dd.yyyy"},
{"[hhmmss]", "HHmmss"},
{"[hh-mm-ss]", "HH-mm-ss"},
{"[hh.mm.ss]", "HH.mm.ss"},
{"[hhmm]", "HHmm"},
{"[hh-mm]", "HH-mm"},
{"[hh.mm]", "HH.mm"},
{"[mmyyyy]", "MMyyyy"},
{"[mm-yyyy]", "MM-yyyy"},
{"[mm.yyyy]", "MM.yyyy"},
{"[yyyymm]", "yyyyMM"},
{"[yyyy-mm]", "yyyy-MM"},
{"[yyyy.mm]", "yyyy.MM"},
{"[ddmm]", "ddMM"},
{"[dd-mm]", "dd-MM"},
{"[dd.mm]", "dd.MM"},
{"[mmdd]", "MMdd"},
{"[mm-dd]", "MM-dd"},
{"[mm.dd]", "MM.dd"}
};
foreach (KeyValuePair<string, string> entry in dateDictionary) {
if (filename.IndexOf(entry.Key, 0, StringComparison.OrdinalIgnoreCase) != -1) {
string formattedDate = (DateTime.Today).ToString(entry.Value);
// 不区分大小写的替换
string result = Regex.Replace(
filename,
Regex.Escape(entry.Key),
formattedDate.Replace("$", "$$"),
RegexOptions.IgnoreCase
);
filename = result;
break;
}
};
return filename;
}
希望这对你有所帮助。如果你需要更多帮助或有其他问题,请随时提出。
英文:
The below code works for all of the custom date format strings except for the ones relating to time. The ToString returns zeroes for any custom format except the ones containing dates. Is it possible to use the custom formats for time in the way that I'm attempting or do I need to do manual manipulation of the string?
protected string PopulateDateInFilename(string filename) {
// The filename can have a date format template, replace template with data here
Dictionary<string, string> dateDictionary = new Dictionary<string, string>() {
{"[yyyymmdd]", "yyyyMMdd"},
{"[yyyy-mm-dd]", "yyyy-MM-dd"},
{"[yyyy.mm.dd]", "yyyy.MM.dd"},
{"[mmddyyyy]", "MMddyyyy"},
{"[mm-dd-yyyy]", "MM-dd-yyyy"},
{"[mm.dd.yyyy]", "MM.dd.yyyy"},
{"[hhmmss]", "HHmmss"},
{"[hh-mm-ss]", "HH-mm-ss"},
{"[hh.mm.ss]", "HH.mm.ss"},
{"[hhmm]", "HHmm"},
{"[hh-mm]", "HH-mm"},
{"[hh.mm]", "HH.mm"},
{"[mmyyyy]", "MMyyyy"},
{"[mm-yyyy]", "MM-yyyy"},
{"[mm.yyyy]", "MM.yyyy"},
{"[yyyymm]", "yyyyMM"},
{"[yyyy-mm]", "yyyy-MM"},
{"[yyyy.mm]", "yyyy.MM"},
{"[ddmm]", "ddMM"},
{"[dd-mm]", "dd-MM"},
{"[dd.mm]", "dd.MM"},
{"[mmdd]", "MMdd"},
{"[mm-dd]", "MM-dd"},
{"[mm.dd]", "MM.dd"}
};
foreach (KeyValuePair<string, string> entry in dateDictionary) {
if (filename.IndexOf(entry.Key, 0, StringComparison.OrdinalIgnoreCase) != -1) {
string formattedDate = (DateTime.Today).ToString(entry.Value);
// case insensitive replace
string result = Regex.Replace(
filename,
Regex.Escape(entry.Key),
formattedDate.Replace("$", "$$"),
RegexOptions.IgnoreCase
);
filename = result;
break;
}
};
return filename;
}
答案1
得分: 1
> 一个被设置为今天日期的对象,时间部分被设置为 00:00:00。
你想要的是 DateTime.Now
,我猜。
英文:
DateTime.Today
returns
> An object that is set to today's date, with the time component set to 00:00:00.
You want DateTime.Now
, I guess.
答案2
得分: 1
DateTime.Today
属性返回当前日期和时间,但是 DateTime 实例的时间部分始终被设为 00:00:00。尝试将 DateTime.Today
更改为 DateTime.Now
,它返回一个 DateTime
对象,该对象被设置为本计算机上的当前日期和时间,以本地时间表示:
foreach (KeyValuePair<string, string> entry in dateDictionary)
{
if (filename.IndexOf(entry.Key, 0, StringComparison.OrdinalIgnoreCase) != -1)
{
string formattedDate = (DateTime.Now).ToString(entry.Value);
// case insensitive replace
string result = Regex.Replace(
filename,
Regex.Escape(entry.Key),
formattedDate.Replace("$", "$$"),
RegexOptions.IgnoreCase);
filename = result;
break;
}
}
英文:
DateTime.Today
property returns the current date and time, but the time portion of the DateTime instance is always set to 00:00:00. Try to fix DateTime.Today
to DateTime.Now
which gets a DateTime
object that is set to the current date and time on this computer, expressed as the local time.:
foreach (KeyValuePair<string, string> entry in dateDictionary)
{
if (filename.IndexOf(entry.Key, 0, StringComparison.OrdinalIgnoreCase) != -1)
{
string formattedDate = (DateTime.Now).ToString(entry.Value);
// case insensitive replace
string result = Regex.Replace(
filename,
Regex.Escape(entry.Key),
formattedDate.Replace("$", "$$"),
RegexOptions.IgnoreCase);
filename = result;
break;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论