Converting time to Military

后端 未结 6 1218
清歌不尽
清歌不尽 2021-01-11 12:40

In an attempt to turn a date with a format of \"mm/dd/yyyy hh:mm:ss PM\" into military time, the following replacement of a row value does not seem to take. Eve

相关标签:
6条回答
  • 2021-01-11 13:11

    Try

    row["adate"].Text = oos.ToString("MM/dd/YYYY HH:mm");
    
    0 讨论(0)
  • 2021-01-11 13:12

    In C# >= 6.0 you can use string interpolation as well if you need to add something around your date. Something like:

    row["adate"] = $"S: {StartDateTime:yyyy-MM-dd HH:mm:ss}";
    
    0 讨论(0)
  • 2021-01-11 13:13

    Simple way is

    bool timeConversion = DateTime.TryParse(s,out DateTime time);
    
    if(timeConversion)
    {
    return time.Tostring("HH:mm:ss");
    }
    

    here be careful to write "HH"

    0 讨论(0)
  • 2021-01-11 13:15

    Beside first answer check this:

    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
    http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx

    0 讨论(0)
  • 2021-01-11 13:22

    Instead of formatting the string manually, you should use:

    oos.ToString("M/d/yyyy HH:mm");
    

    Also, what do you mean by "would not accept a value"? Are you getting an exception? If so, what is the error message?

    0 讨论(0)
  • 2021-01-11 13:24

    If you have this time: 07:12:02PM and you want this: 19:12:02PM, then this is the code for you!

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    class Solution {
    
        static string timeConversion(string s) {
           DateTime dateTime = DateTime.ParseExact(s, "hh:mm:sstt", 
            System.Globalization.CultureInfo.InvariantCulture); 
    
            return (dateTime.ToString("HH:mm:ss"));
        }
    
        static void Main(String[] args) {
            string s = Console.ReadLine(); 
            string result = timeConversion(s);
            Console.WriteLine(result);
        }
    }
    
    0 讨论(0)
提交回复
热议问题