Split a DateTime Variable

Hi there,

I am assigning a DateTime variable to get the current date and time using System.DateTime.Now. When I do this, the following example appears:

09/04/2019 09:51:05

I would now like to split this value by the space so that I only get the time (09:51:05). I then want to assign this time to another datetime variable that I have created (StartTime). However, I am having trouble because neither of the variables are string variables. Any help would be greatly appreciated.

1 Like

Why are you putting this in a new variable? Just do startTime = System.DateTime.Now and reference that later.

We are creating two separate variables for analytics purposes. Both values will be inserted into a SQL data table, then imported into a data visualization software. In order to model some of the data that we need, both sets of values are required.

One variable will be the date + time and the other will just be the time?

@mgartner Follow below solution

Assign string v1 = “09/04/2019 09:51:05”

Assign string StartTime = v1.Split(" ".ToCharArray)(1)

So now StartTime will have 09:51:05

Yes that’s correct

dateTime = System.DateTime.Now
time = dateTime.ToString("HH:mm:ss")

Will give you the current date & time as a DateTime and the current time as a string.

This worked. Thank you!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.