Ishtiaq
Ishtiaq

Reputation: 1058

Converting a time string to append with DateTime object

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.

Upvotes: 4

Views: 1378

Answers (5)

Soner Gönül
Soner Gönül

Reputation: 98750

12:48 AM is not a TimeSpan, it is a time part of a DateTime. You need to parse it to DateTime, not TimeSpan.

You can use to add .TimeOfDay property of your time and add it to date. This property returns only time part of your DateTime as a TimeSpan.

string timeValue = "12:48 AM";
var time = DateTime.ParseExact(timeValue, "h:mm tt", CultureInfo.InvariantCulture);
DateTime launchDate = date + time.TimeOfDay;

Upvotes: 1

Matthias
Matthias

Reputation: 1437

If you want a DateTime object

 string timeValue = "10:48 AM";
 string timeFormate = "h:mm tt";
 var dateTime = DateTime.ParseExact(timeValue, timeFormate, CultureInfo.InvariantCulture);

edit: if you want to add a timespan to a given DateTime object you should skip the "AM/PM"

string timeValue = "2:30";
DateTime launchTime = DateTime.Now;
TimeSpan timeSpan;
if (TimeSpan.TryParse(timeValue, out timeSpan))
{
   launchTime = launchTime.Add(timeSpan);
}

BR

Upvotes: 1

B.K.
B.K.

Reputation: 10152

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

Upvotes: 4

Banusundar Arumugam
Banusundar Arumugam

Reputation: 251

Check this Page from MSDN, this could help u

http://msdn.microsoft.com/en-us/library/dd992370(v=vs.110).aspx

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can parse it to DateTime and use it's TimeOfDay property to get the time:

DateTime time = DateTime.ParseExact("12:48 AM", "h:mm tt", CultureInfo.InvariantCulture);
DateTime launchDate = date + time.TimeOfDay;

Note that i've also changed the format string since you need tt for the AM/PM designator.

Upvotes: 3

Related Questions