Change date format and keep times

Hello,

I am currently trying to change the format of a date whilst keeping the times following it. I am currently using DateTime.Now to set a date but it is coming in “mm/dd/yyyy” and the following time. I need it to be “dd/mm/yyyy” and have a time following it. Any ideas how I could achieve this?

I have tried DateTime.Now.ToString(“dd/MM/yyyy”) but I need the times to follow the date to get a total run time.

Any feedback would be appreciated.

Thanks in advance!

1 Like

@william.coulson

Try this.

   DateTime.Now.ToString(“dd/MM/yyyy HH:mm:ss”)
1 Like

Hi @william.coulson,

use below format
"dd/MM/yyyy hh:mm:ss tt"

Regards,
Arivu

1 Like

@lakshman,

That’s worked, thank you. Do I need to use FinishTime - StartTime to get a total time? FinishTime.Subtract(StartTime) has a validation error.

1 Like

can you send the error screenshort

TotalTimeSpan= **FinishTime.Subtract(StartTime)**

1 Like

@arivu96,

I’ve tried these two options, that work with DateTime. Do I need to try with -?

Make it both the variable are datetime format.
Else convert to date format
Convert.ToDateTime(FinishTime).Subtract(Convert.ToDateTime(StartTime))

Regards,
Arivu

1 Like

@arivu96,

That’s worked, than you very much.

Hey @arivu96,

The solution you’ve given me isn’t working now. Any ideas why? Have tried with FinishTime and StartTime as String and GenericValue and neither worked.

This is the error…

image

1 Like

@Palaniyappan, any ideas? Trying to subtract dates in string form.

1 Like

sure buddy
But may i know where we are gettting the input for date
@william.coulson

@Palaniyappan,

At the moment, I am getting StartDate and Finish Date from DateTime.Now.ToString(“dd/MM/yyyy hh:mm:ss”). I need to get a total time from subtracting these two variables. I am using @arivu96’s solution:

Which is coming up with an error, screenshotted above

1 Like

Great
so the expression be like this for different scenarios
date_start = Datetime.ParseExact(StartDate.ToString,“dd/MM/yyyy hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)

date_finish = Datetime.ParseExact(StartDate.ToString,“dd/MM/yyyy hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)

date_start and date_finish is a variable of type System.Datetime

nw
for day difference
int_date = (date_start-date_finish).TotalDays

for minutes difference
int_minutes = (date_start-date_finish).TotalMinutes

for seconds difference
int_seconds = (date_start-date_finish).TotalSeconds

Cheers @william.coulson

4 Likes

@Palaniyappan,

Can I get hours using int_date = (date_start-date_finish).TotalHours?

1 Like

yah of course
int_hours= (date_start-date_finish).TotalHours

Cheers @william.coulson

@Palaniyappan,

I assume the variables are Integers? Silly question

1 Like

as System.Double
Cheers
@william.coulson

1 Like

Were we able to get them now
@william.coulson

@Palaniyappan,

I have set the date format to dd/MM/yyyy and it is still outputting as MM/dd/yyyy. Time is correct though.

StartDate is date_start

In DateTime.ParseExact atlast include .ToString(“dd/MM/yyyy hh:mm:ss”)

Cheers @william.coulson