Languages
[Edit]
EN

C# / .NET - get current timestamp

7 points
Created by:
Root-ssh
6442

In this article, we're going to have a look at how to get current timestamp in C# / .NET. There are available two ways to do it: use predefined methods or use custom formatting.

Quick solution:

DateTimeOffset now = (DateTimeOffset)DateTime.UtcNow;  // using System;

Console.WriteLine(now.ToUnixTimeSeconds());            // .NET 4.6

Note: read this article to see iso 8601 examples.

Go to below sections to see more examples:

1. Custom Unix Time in seconds example

If we want to have source code that works with older .net frameworks we should use this solution.

using System;

public static class TimeUtils
{
	public static int ToUnixTimeSeconds(DateTime date)
	{
		DateTime point = new DateTime(1970, 1, 1);
		TimeSpan time = date.Subtract(point);

		return (int)time.TotalSeconds;
	}

	public static int ToUnixTimeSeconds()
	{
		return ToUnixTimeSeconds(DateTime.UtcNow);
	}
}

// Usage example:

public class Program
{
	public static void Main()
	{
		Console.WriteLine(TimeUtils.ToUnixTimeSeconds());
	}
}

Output:

1565641706

2. .NET 4.6 API Unix timestamp example

.net 4.6 introduced special method that returns Unix timestamp.

using System;

public class Program
{
	public static void Main()
	{
		DateTimeOffset now = (DateTimeOffset)DateTime.UtcNow;

		Console.WriteLine(now.ToUnixTimeSeconds());
	}
}

Output:

1565642183

3. Predefined DateTime class methods example

Predefined methods available in .net return most commonly used time formats.

Note: it is very important to look on currently used CultureInfo to know what kind of formatting is used by default. It is possible to call to string method with desired culture.

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		DateTime now = DateTime.Now;

		CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

		Console.WriteLine(now.ToBinary());            // -8586359643687607670
		Console.WriteLine(now.ToFileTime());          // 132101161167168138
		Console.WriteLine(now.ToFileTimeUtc());       // 132101161167168138
		Console.WriteLine(now.ToLocalTime());         // 08/12/2019 21:41:56
		Console.WriteLine(now.ToLongDateString());    // Monday, 12 August 2019
		Console.WriteLine(now.ToLongTimeString());    // 21:41:56
		Console.WriteLine(now.ToOADate());            // 43689.9041286574
		Console.WriteLine(now.ToShortDateString());   // 08/12/2019
		Console.WriteLine(now.ToShortTimeString());   // 21:41
		Console.WriteLine(now.ToString());            // 08/12/2019 21:41:56
	}
}

Output:

-8586359643687607670
132101161167168138
132101161167168138
08/12/2019 21:41:56
Monday, 12 August 2019
21:41:56
43689.9041286574
08/12/2019
21:41
08/12/2019 21:41:56
Note: CultureInfo.CurrentCulture property causes specific string formatting for numbers, currencies, time, etc.

4. ToString method with custom format example

In this section we presented how to use custom format to print formatted date and time - format can be modified if it is necessary.

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		DateTime dt = new DateTime(2018, 2, 15, 8, 21, 34, 547);
		CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

		Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm"));
		Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm tt"));
	}
}

Output:

Thursday, 15 February 2018 08:21
Thursday, 15 February 2018 08:21 AM

For different ToString format, results look following way: 

/*

FORMAT                            RESULT

dddd, dd MMMM yyyy HH:mm          Thursday, 15 February 2018 08:21
dddd, dd MMMM yyyy HH:mm tt       Thursday, 15 February 2018 08:21 AM
dddd, dd MMMM yyyy HH:mm:ss       Thursday, 15 February 2018 08:21:34
dddd, dd MMMM yyyy HH:mm:ss tt    Thursday, 15 February 2018 08:21:34 AM
dddd, dd MMMM yyyy H:mm           Thursday, 15 February 2018 8:21
dddd, dd MMMM yyyy H:mm tt        Thursday, 15 February 2018 8:21 AM
dddd, dd MMMM yyyy H:mm:ss        Thursday, 15 February 2018 8:21:34
dddd, dd MMMM yyyy H:mm:ss tt     Thursday, 15 February 2018 8:21:34 AM
dddd, dd MMMM yyyy hh:mm          Thursday, 15 February 2018 08:21
dddd, dd MMMM yyyy hh:mm tt       Thursday, 15 February 2018 08:21 AM
dddd, dd MMMM yyyy hh:mm:ss       Thursday, 15 February 2018 08:21:34
dddd, dd MMMM yyyy hh:mm:ss tt    Thursday, 15 February 2018 08:21:34 AM
dddd, dd MMMM yyyy h:mm           Thursday, 15 February 2018 8:21
dddd, dd MMMM yyyy h:mm tt        Thursday, 15 February 2018 8:21 AM
dddd, dd MMMM yyyy h:mm:ss        Thursday, 15 February 2018 8:21:34
dddd, dd MMMM yyyy h:mm:ss tt     Thursday, 15 February 2018 8:21:34 AM
MM/dd/yyyy HH:mm                  02/15/2018 08:21
MM/dd/yyyy HH:mm tt               02/15/2018 08:21 AM
MM/dd/yyyy HH:mm:ss               02/15/2018 08:21:34
MM/dd/yyyy HH:mm:ss tt            02/15/2018 08:21:34 AM
MM/dd/yyyy H:mm                   02/15/2018 8:21
MM/dd/yyyy H:mm tt                02/15/2018 8:21 AM
MM/dd/yyyy H:mm:ss                02/15/2018 8:21:34
MM/dd/yyyy H:mm:ss tt             02/15/2018 8:21:34 AM
MM/dd/yyyy hh:mm                  02/15/2018 08:21
MM/dd/yyyy hh:mm tt               02/15/2018 08:21 AM
MM/dd/yyyy hh:mm:ss               02/15/2018 08:21:34
MM/dd/yyyy hh:mm:ss tt            02/15/2018 08:21:34 AM
MM/dd/yyyy h:mm                   02/15/2018 8:21
MM/dd/yyyy h:mm tt                02/15/2018 8:21 AM
MM/dd/yyyy h:mm:ss                02/15/2018 8:21:34
dddd, dd MMMM yy                  Thursday, 15 February 18
dddd, dd MMMM yyyy                Thursday, 15 February 2018
MM/dd/yy                          02/15/18
MM/dd/yyyy                        02/15/2018
MMMM dd                           February 15
yy-MM-dd                          18-02-15
yyyy-MM-dd                        2018-02-15
yy.MM.dd                          18.02.15
yyyy.MM.dd                        2018.02.15
dd.MM.yy                          15.02.18
dd.MM.yyyy                        15.02.2018
yyyy MMMM                         2018 February
ddd, MMM d, yyyy                  Thu, Feb 15, 2018
dddd, MMMM d, yyyy                Thursday, February 15, 2018
yyyy-MM-ddTHH:mm:ss               2018-02-15T08:21:34
yyyy-MM-ddTHH:mm:ss.fffffffK      2018-02-15T08:21:34.5470000
yyyyMMddHHmmss                    20180215082134
yyyyMMddHHmmssfffffff             201802150821345470000
ddd, dd MMM yyy HH:mm:ss 'GMT'    Thu, 15 Feb 2018 08:21:34 GMT
HH:mm                             08:21
HH:mm tt                          08:21 AM
HH:mm:ss                          08:21:34
HH:mm:ss tt                       08:21:34 AM
H:mm                               8:21
H:mm tt                            8:21 AM
H:mm:ss                            8:21:34
H:mm:ss tt                         8:21:34 AM
hh:mm                             08:21
hh:mm tt                          08:21 AM
hh:mm:ss                          08:21:34
hh:mm:ss tt                       08:21:34 AM
h:mm                               8:21
h:mm tt                            8:21 AM
h:mm:ss                            8:21:34


Where:
       H -  0-23 hours
      HH - 00-23 hours         (zeros are printed to keep two places)
       h -  0-12 hours
      hh - 00-12 hours         (zeros are printed to keep two places)
      mm - 00-59 minutes       (zeros are printed to keep two places)
      ss - 00-59 seconds       (zeros are printed to keep two places)
      tt - AM or PM
 fffffff - second fraction
fffffffK - second fraction

       d - day of month number
      dd - day of month number (zeros are printed to keep two places)
     ddd - short day name
    dddd - long day name
      MM - month number        (zeros are printed to keep two places)
     MMM - short month name
    MMMM - long month name
      yy - 00-99 year          (zeros are printed to keep two places)
     yyy - 00-999 year         (zeros are printed to keep two places)
    yyyy - 00-9999 year        (zeros are printed to keep two places)

  Charactes that are not interpreted: T - : . ,

  'text' - single apostrophe does not interpret text inside e.g. 'GMT' as GMT

*/

References:

  1. DateTimeOffset.ToUnixTimeSeconds Method - Microsoft Docs
  2. DateTime Class - Microsoft Docs
  3. CultureInfo.CurrentCulture property - Microsoft Docs
  4. Custom date and time format strings - Microsoft Docs
  5. Standard Date and Time Format Strings - Microsoft Docs
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.