How to get current date time in specific format

Hi, in text file I am trying to print current date and time.

I am using below code

System.DateTime.Now.ToString("dd-mm-yyyy hh:mm:ss")

and getting below results, you can see results are changing every time for month.

17-13-2019 08:13:59
17-14-2019 08:14:22

But if I use System.DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss")

I am getting the correct result, see below:

17-09-2019 08:14:45

I am not able to understand why it is happening, if I just use the month in uppercase why it is working fine and why not with small case. If I put only month in uppercase code is not looking so good in formatting way, also if I put all in upper case
System.DateTime.Now.ToString("DD-MM-YYYY HH:MM:SS")
then I am getting below result:

DD-09-YYYY 20:09:SS

Unable to understand the behavior of this. Can anyone please explain this.

17 Likes

hi
welcome to UiPath community
usually when mm means minutes and MM means Month
So in the first expression we were getting the minutes in place of month
17-13-2019 08:13:59
17-14-2019 08:14:22

and after changing the value to MM
we get the right value
17-09-2019 08:14:45 (see the difference)

so the usual format be like this
dd-MM-yyyy hh:mm:ss

where
dd - date
MM - month in number
MMM - month like Jan, Feb
MMMM - month like January, February
yyyy - 2019
yy-19

and in timestamp part
hh - hours in 12 hr format
HH - hours in 24 hr format
mm - minutes
ss - seconds
tt - AM or PM (if needed at last like this dd-MM-yyyy hh:mm:ss tt)

hope this would help you @ShenkiSinghai

Cheers @ShenkiSinghai

57 Likes

mm gives you minutes and

MM is for months @ShenkiSinghai

4 Likes

Yes, you are right.
But nothing is wrong with Uipath studio here, Its dot net thing that we are following from very long time

So, I usually use Now.ToString(“dd-MM-yyyy hh:mm”

I know but it looks awkward but nevermind just going with flow of dotnet

3 Likes

@Palaniyappan Many Thanks. Can you please also confirm why we are not getting for seconds when we use SS, also for date and year with DD and YYYY. Is this only work with small caps?

3 Likes

Thanks @HareeshMR

2 Likes

seconds, date and year are to be mentioned in small cases like this
ss, dd and yyyy

for more info on this

Cheers @ShenkiSinghai

5 Likes

Thanks @Prafull_B

3 Likes

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