Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display microseconds of current date time in c#. following code works correctly
in my windows 7.
currentDateString = string.Format("{0}", serverDate.ToString("yyyyMMddHHmmssffffff"));


but not work correctly at server.
at server display
yyyyMMddHHmmss000000

how can i solve it ?

What I have tried:

i try following .
currentDateString = string.Format("{0}{1}", serverDate.ToString("yyyyMMddHHmmss"), serverDate.ToString("ffffff"));
and result is :
yyyyMMddHHmmss000000
Posted
Updated 26-May-17 22:15pm

1 solution

When I try it:
DateTime serverDate = DateTime.Now;
string currentDateString = string.Format("{0}", serverDate.ToString("yyyyMMddHHmmssffffff"));
Console.WriteLine(currentDateString);
I get what I expect:
20170527091049145901
yyyyMMddHHmmssffffff

So I'd guess that the "serverDate" value you are fetching (from where, I don't know) does not have a component below the second.
Use the debugger to look at exactly what it contains, and if the Millisecond property is 0, then that's why it prints as "000000" when you expect a value.
You can then look at the DateTime source to find out why.


"how can i get Microseconds of current date time by T-sql ?"


Same way you get any other info: just read it into a DateTime variable:
try
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 InsertDate, GETDATE() FROM Videos", con))
            {
            DataTable dt = new DataTable();
            using (SqlDataReader reader = cmd.ExecuteReader())
                {
                while (reader.Read())
                    {
                    DateTime dt1, dt2;
                    dt1 = (DateTime)reader[0];
                    dt2 = (DateTime)reader[1];
                    string dt1s = string.Format("{0}", dt1.ToString("yyyyMMddHHmmssffffff"));
                    string dt2s = string.Format("{0}", dt2.ToString("yyyyMMddHHmmssffffff"));
                    Console.WriteLine("{0}:{1}", dt1s, dt2s);
                    }
                }
            }
        }
    }
catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
When I run this, I get:
20120624125436677000:20170527113211523000
20120527083918187000:20170527113211523000
20120527084000500000:20170527113211523000
20160603074757507000:20170527113211523000
20141012123141300000:20170527113211523000
Which indicates that SQL Server ticks are to the millisecond
 
Share this answer
 
v2
Comments
aliwpf 27-May-17 5:56am    
thanks .my source of date time doesn't contain seconds, miliseconds and microseconds.
how can i get microsecond of current date time by T-Sql (my source of datetime)?
OriginalGriff 27-May-17 6:04am    
You're welcome!
aliwpf 27-May-17 6:07am    
how can i get Microseconds of current date time by T-sql ?
OriginalGriff 27-May-17 6:37am    
Answer updated
aliwpf 27-May-17 7:20am    
well done.thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900