16

How can I get a DateTime based on a string

e.g: if I have mytime = "14:00"

How can I get a DateTime object with current date as the date, unless current time already 14:00:01, then the date should be the next day.

1
  • If you are a targeting a particular timezone, you are better off targeting the timezone. Rather than a specific time. Depending on the you application and if it spans timezones a library will protect you from changes in timezones and will make dealing with time and dates sooo much easier. Commented Mar 21, 2016 at 3:01

4 Answers 4

18

This is as simple as parsing a DateTime with an exact format.

Achievable with

var dateStr = "14:00";
var dateTime = DateTime.ParseExact(dateStr, "H:mm", null, System.Globalization.DateTimeStyles.None);

The DateTime.ParseExact() (msdn link) method simply allows you to pass the format string you wish as your parse string to return the DateTime struct. Now the Date porition of this string will be defaulted to todays date when no date part is provided.

To answer the second part

How can I get a DateTime object with current date as the date, unless current time already 14:00:01, then the date should be the next day.

This is also simple, as we know that the DateTime.ParseExact will return todays date (as we havevnt supplied a date part) we can compare our Parsed date to DateTime.Now. If DateTime.Now is greater than our parsed date we add 1 day to our parsed date.

var dateStr = "14:00";

var now = DateTime.Now;
var dateTime = DateTime.ParseExact(dateStr, "H:mm", null, System.Globalization.DateTimeStyles.None);

if (now > dateTime)
    dateTime = dateTime.AddDays(1);
Sign up to request clarification or add additional context in comments.

Comments

7

You can use DateTime.TryParse(): which will convert the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.

string inTime="14:00";
if(DateTime.TryParse(inTime,out DateTime dTime))
{
   Console.WriteLine($"DateTime : {dTime.ToString("dd-MM-yyyy HH:mm:SS")}");
} 

Working example here

Comments

2

There is a datetime constructor for

public DateTime(
    int year,
    int month,
    int day,
    int hour,
    int minute,
    int second
)

So then parse the string to find the hours, minutes, and seconds and feed that into this constructor with the other parameters supplied by Datetime.Now.Day and so on.

Comments

2

I think you want to do something like this:

string myTime = "14:00";            
var v = myTime.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
DateTime obj = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, int.Parse(v[0]), int.Parse(v[1]), DateTime.Now.Second);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.