[Solved] Datetime does not contain definition for “ToTimeStamp”


The docs state here (see also here) that extension methods

[need to be] defined inside a non-nested, non-generic static class

so in order to make the code example work, we need to put the ToTimestamp method inside a static class.

public static class DateTimeExtensions
{
    private static readonly DateTime _jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public static long ToTimestamp(this DateTime d) 
    {
        return (long)(d.ToUniversalTime() - _jan1St1970).TotalSeconds;
    }
}

Note that the class can be named however you want to.


Then, the ToTimeStamp method can be used like this:

public class PasswordHelper
{
    public static string GenerateEncPassword(string password, string publicKey, string keyId, string version) 
    {
        var time = DateTime.UtcNow.ToTimestamp(); // Unix timestamp
        ...
    }
}

solved Datetime does not contain definition for “ToTimeStamp”