Converting a time string to append with DateTime object

Ishtiaq

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.

B.K.

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

分類Dev

How to remove time segment completely from datetime after converting to string?

分類Dev

Converting string to DateTime safely

分類Dev

Converting String to Datetime2

分類Dev

bigquery converting the string datetime with timezone

分類Dev

Converting a string to an object reference?

分類Dev

Converting JSON string into object

分類Dev

Converting DateTime to time ago in Dart/Flutter

分類Dev

Converting time in java with string manipulation

分類Dev

converting object to datetime64[ns]

分類Dev

Converting dataframe object to date using to_datetime

分類Dev

DateTime Object to string

分類Dev

Alternative to eval for converting a string to an object

分類Dev

Converting a time String to ISO 8601 format

分類Dev

Converting String to Time without Date in Java

分類Dev

Java Convert String into datetime Object

分類Dev

Converting object with AM/PM to datetime in hours round down

分類Dev

How can I append a string to an object property?

分類Dev

DateTime object and displaying date/time in alternate timezone

分類Dev

Manipulate time on System.DateTime object

分類Dev

Parse csv object time to datetime in python

分類Dev

c# detect if the string is long or DateTime before converting

分類Dev

descriptor 'time' of 'datetime.datetime' object needs an argument

分類Dev

Julia append!() Cannot `convert` an object of type Char to an object of type String

分類Dev

Object must be a Date, DateTime or Time object. nil given

分類Dev

read in string as datetime object with 3 digits for millisecond

分類Dev

Converting string date to epoch time not working with Cython and POSIX C libraries

分類Dev

converting a muti-time formatted string into seconds (pandas)

分類Dev

Issue converting Boolean to String (works some of the time but not all)

Related 関連記事

  1. 1

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  2. 2

    How to remove time segment completely from datetime after converting to string?

  3. 3

    Converting string to DateTime safely

  4. 4

    Converting String to Datetime2

  5. 5

    bigquery converting the string datetime with timezone

  6. 6

    Converting a string to an object reference?

  7. 7

    Converting JSON string into object

  8. 8

    Converting DateTime to time ago in Dart/Flutter

  9. 9

    Converting time in java with string manipulation

  10. 10

    converting object to datetime64[ns]

  11. 11

    Converting dataframe object to date using to_datetime

  12. 12

    DateTime Object to string

  13. 13

    Alternative to eval for converting a string to an object

  14. 14

    Converting a time String to ISO 8601 format

  15. 15

    Converting String to Time without Date in Java

  16. 16

    Java Convert String into datetime Object

  17. 17

    Converting object with AM/PM to datetime in hours round down

  18. 18

    How can I append a string to an object property?

  19. 19

    DateTime object and displaying date/time in alternate timezone

  20. 20

    Manipulate time on System.DateTime object

  21. 21

    Parse csv object time to datetime in python

  22. 22

    c# detect if the string is long or DateTime before converting

  23. 23

    descriptor 'time' of 'datetime.datetime' object needs an argument

  24. 24

    Julia append!() Cannot `convert` an object of type Char to an object of type String

  25. 25

    Object must be a Date, DateTime or Time object. nil given

  26. 26

    read in string as datetime object with 3 digits for millisecond

  27. 27

    Converting string date to epoch time not working with Cython and POSIX C libraries

  28. 28

    converting a muti-time formatted string into seconds (pandas)

  29. 29

    Issue converting Boolean to String (works some of the time but not all)

ホットタグ

アーカイブ