简体   繁体   English

转换时间字符串以附加DateTime对象

[英]Converting a time string to append with DateTime object

I have a time string as 12:48 AM . 我有一个时间字符串作为12:48 AM I want to convert this string into TimeSpan to append with DateTime Object. 我想将此字符串转换为TimeSpan以附加DateTime对象。 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: 您需要将该时间解析为DateTime ,然后在追加到原始日期时简单地从中提取TimeOfDay

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: OUTPUT:

3/10/1987 12:00:00 AM
11/12/2014 12:48:00 AM
3/10/1987 12:48:00 AM

You can parse it to DateTime and use it's TimeOfDay property to get the time: 您可以将其解析为DateTime并使用它的TimeOfDay属性来获取时间:

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. 请注意,我也改变了格式字符串,因为你需要tt的AM / PM指示。

12:48 AM is not a TimeSpan , it is a time part of a DateTime . 12:48 AM不是TimeSpan ,它是DateTime的时间部分。 You need to parse it to DateTime , not TimeSpan . 您需要将其解析为DateTime ,而不是TimeSpan

You can use to add .TimeOfDay property of your time and add it to date . 您可以使用添加时间的.TimeOfDay属性并将其添加到date This property returns only time part of your DateTime as a TimeSpan . 此属性仅返回DateTime时间部分作为TimeSpan

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

Check this Page from MSDN, this could help u 从MSDN检查此页面,这可以帮助你

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

If you want a DateTime object 如果需要DateTime对象

 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" 编辑:如果要向给定的DateTime对象添加时间跨度,则应跳过“AM / PM”

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

BR BR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM