I have a time string as 12:48 AM
. I want to convert this string into TimeSpan
to append with DateTime
Object. Currently I am trying the following snippets.
string format = "dd/MM/yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
var date = DateTime.ParseExact(dateValue, format, provider);
string timeFormate = "H:mm AM";
string timeValue = "12:48 AM";
var time = TimeSpan.ParseExact(timeValue,timeFormate,provider);
DateTime launchDate = date + time;
I am getting
Input string was not in a correct format
exception at line
var time = TimeSpan.ParseExact(timeValue,timeFormate,provider);
Please suggest me how to convert my specified string into time.
You need to parse that time into DateTime
and then simply extract the TimeOfDay
out of it when appending to the original date:
using System;
using System.Globalization;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
var dateValue = "10/03/1987";
var date = DateTime.ParseExact(dateValue, "dd/MM/yyyy", CultureInfo.InvariantCulture);
var timeValue = "12:48 AM";
var time = DateTime.ParseExact(timeValue, "h:mm tt", CultureInfo.InvariantCulture);
var dateTime = date + time.TimeOfDay;
Console.WriteLine(date);
Console.WriteLine(time);
Console.WriteLine(dateTime);
}
}
}
OUTPUT:
3/10/1987 12:00:00 AM
11/12/2014 12:48:00 AM
3/10/1987 12:48:00 AM
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加