如何使用自定义的 DateTime 字符串格式来表示时间,而不包括日期部分?

huangapple go评论135阅读模式
英文:

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&lt;string, string&gt; dateDictionary = new Dictionary&lt;string, string&gt;() {
            {&quot;[yyyymmdd]&quot;,  &quot;yyyyMMdd&quot;},
            {&quot;[yyyy-mm-dd]&quot;,  &quot;yyyy-MM-dd&quot;},
            {&quot;[yyyy.mm.dd]&quot;,  &quot;yyyy.MM.dd&quot;},
            {&quot;[mmddyyyy]&quot;,  &quot;MMddyyyy&quot;},
            {&quot;[mm-dd-yyyy]&quot;,  &quot;MM-dd-yyyy&quot;},
            {&quot;[mm.dd.yyyy]&quot;,  &quot;MM.dd.yyyy&quot;},
            {&quot;[hhmmss]&quot;,  &quot;HHmmss&quot;},
            {&quot;[hh-mm-ss]&quot;,  &quot;HH-mm-ss&quot;},
            {&quot;[hh.mm.ss]&quot;,  &quot;HH.mm.ss&quot;},
            {&quot;[hhmm]&quot;,  &quot;HHmm&quot;},
            {&quot;[hh-mm]&quot;,  &quot;HH-mm&quot;},
            {&quot;[hh.mm]&quot;,  &quot;HH.mm&quot;},
            {&quot;[mmyyyy]&quot;,  &quot;MMyyyy&quot;},
            {&quot;[mm-yyyy]&quot;,  &quot;MM-yyyy&quot;},
            {&quot;[mm.yyyy]&quot;,  &quot;MM.yyyy&quot;},
            {&quot;[yyyymm]&quot;,  &quot;yyyyMM&quot;},
            {&quot;[yyyy-mm]&quot;,  &quot;yyyy-MM&quot;},
            {&quot;[yyyy.mm]&quot;,  &quot;yyyy.MM&quot;},
            {&quot;[ddmm]&quot;,  &quot;ddMM&quot;},
            {&quot;[dd-mm]&quot;,  &quot;dd-MM&quot;},
            {&quot;[dd.mm]&quot;,  &quot;dd.MM&quot;},
            {&quot;[mmdd]&quot;,  &quot;MMdd&quot;},
            {&quot;[mm-dd]&quot;,  &quot;MM-dd&quot;},
            {&quot;[mm.dd]&quot;,  &quot;MM.dd&quot;}
        };

        foreach (KeyValuePair&lt;string, string&gt; 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(&quot;$&quot;, &quot;$$&quot;),
                    RegexOptions.IgnoreCase
                );
                filename = result;
                break;
            }
        };

        return filename;
    }

答案1

得分: 1

DateTime.Today 返回

> 一个被设置为今天日期的对象,时间部分被设置为 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&lt;string, string&gt; 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(&quot;$&quot;, &quot;$$&quot;),
                    RegexOptions.IgnoreCase);
        filename = result;
        break;
    }
}

huangapple
  • 本文由 发表于 2023年2月14日 03:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定