Jump to content

C# - Why does my time function somtimes return 3 digits?

SoftwareNinja

Hi there,

 

Within my C# project I have a function that gets the time now in a double. However, in 4 occasions the output to the database has been 3 digits.

 

Here is the code:

public double SQRGetTimeNow()
        {
            var timezone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
            var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);

            double timeInt = Convert.ToDouble(dateTime.ToString("HH:mm:ss").Replace(":", ""));

            return timeInt;
        }

 

This function should always return the likes of 13:56:32 = 135632

or 05:45:32 = 054532

 

When saving the 5am time above to the database it remove the zero (expected)

image.png.ce253ee05277693677c9f0b7b40c824a.png - First 4 are missing the zero in front of them but I have a converter for placing the zero there when displaying.

 

But 4 times this function has returned 3 or 4 digit results and output them to the database?

image.png.24e23823bc2c180eb088804fd3bdf005.png

 

Anyone have an idea why? 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, zhnu said:

You're converting to int so 00:01:01 would give you an LoggedTime 101. Also you should never save dates on a DB without timezone, also databases usually have their own time functions you should use that.
 

Okay awesome! I will take a deeper look and reconsider my options. TimeZone is something we are implementing now also. Thanks for the tip!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×