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
Try
row["adate"].Text = oos.ToString("MM/dd/YYYY HH:mm");
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}";
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"
Beside first answer check this:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx
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?
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);
}
}