How to add literal strings in a DateTime format?

后端 未结 4 1380
情书的邮戳
情书的邮戳 2020-12-16 09:07

Is it possible to add the word \"at\" between a Date and Time format? I tried to add it just like this \"ffffdd, d MMM, yyyy at HH:mm\" but the webapp is tran

相关标签:
4条回答
  • 2020-12-16 09:41

    The other answers are far better, but:

    var now = DateTime.Now;
    
    var str = now.ToString("d MMM yyyy") + " at " + now.ToString("HH:mm");
    

    or closer to your formatting:

    var str = now.ToString("ffffdd, d MMM, yyyy") + " at " + now.ToString("HH:mm");
    
    0 讨论(0)
  • 2020-12-16 09:42
    Console.WriteLine(DateTime.Now.ToString("ffffdd, d MMM, yyyy 'at' HH:mm"));
    
    0 讨论(0)
  • 2020-12-16 09:52

    Yes, you need to escape the word by putting it in ' marks ffffdd, d MMM, yyyy 'at' HH:mm

    Custom DateTime string formatting

    0 讨论(0)
  • 2020-12-16 09:52

    Improving @DaveDev answer using C# 6's String Interpolation

    var now = DateTime.Now;
    Console.WriteLine($"{now:ffffdd, d MMM, yyyy} at {now:HH:mm}");
    
    0 讨论(0)
提交回复
热议问题