Stef Heyenrath
Stef Heyenrath

Reputation: 9830

How to convert year, month and day to ticks without using DateTime

long ticks = new DateTime(2012, 1, 31).ToLocalTime().Ticks; // 634635684000000000

But how to do this without DateTime constructor ?

edit
What I actually want is to keep only the years, months and days from the ticks.

long ALL_Ticks = DateTime.Now.Ticks; // 634636033446495283
long Only_YearMonthDay = 634635684000000000; // how to do this ?

I want to use this in a linq-sql query using Linq.Translations.

Upvotes: 0

Views: 10300

Answers (6)

Steven Turner
Steven Turner

Reputation: 156

I had a use case where I couldn't use DateTime but needed Years/Months from Ticks.

I used the source behind DateTime to figure out how. To go the other way you can look at the constructor, one of which calls the following code.

    private static long DateToTicks(int year, int month, int day) {
        if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) {
            int[] days = IsLeapYear(year)? DaysToMonth366: DaysToMonth365;
            if (day >= 1 && day <= days[month] - days[month - 1]) {
                int y = year - 1;
                int n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
                return n * TicksPerDay;
            }
        }
        throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_BadYearMonthDay"));
    }

This can be found in link below, of course you will need to re-write to suit your needs and look up the constants and IsLeapYear function too.

https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,602

Upvotes: 1

Martin Hennings
Martin Hennings

Reputation: 16856

You already have the answer there in your post:

long ALL_Ticks = DateTime.Now.Ticks; 
// that's the ticks (from DateTime.MinValue) until 'now' (this very moment)

long ticks = new DateTime(2012, 1, 31).ToLocalTime().Ticks;
// or
long ticks = DateTime.Now.Date.Ticks;
// that's the ticks until the beginning of today

long yearmonthticks = new DateTime(2012, 1, 1).ToLocalTime().Ticks;
// that's the ticks until the beginning of the month

// etc..., the rest is simple subtractions

Since your question doesn't specify any reason not to use the DateTime constructor, this is the best solution for what seems like your problem.

Upvotes: 0

H27studio
H27studio

Reputation: 467

I understand you dont want the hour parts of the date. If you use Date, then you only get the day (for example: 01/01/2012 00:00:00)

long ticks = new DateTime(2012, 1, 31).Date.Ticks;

And with any DateTime object already created is the same of course.

long ticks = dateObject.Date.Ticks;

Upvotes: 0

jfiskvik
jfiskvik

Reputation: 645

If you only want the ticks for the date portion of the current datetime you could use:

    long Only_YearMonthDay = DateTime.Now.Date.Ticks; //634635648000000000
    //DateTime.Now.Date.Ticks + DateTime.Now.TimeOfDay.Ticks == DateTime.Now.Ticks

Upvotes: 3

Bertie
Bertie

Reputation: 733

You could find out how many days are in the calculation and then multiply by 864,000,000,000 (which is how many ticks are in a day). Is that what you are looking for? Bit of documentation here : http://msdn.microsoft.com/en-us/library/system.timespan.ticksperday.aspx.

Happy coding,
Cheers,
Chris.

OK - didn't think this through properly! Ticks represent the amount of 100 nanosecond intervals since 12:00:00 midnight, January 1, 0001. You would need to calculate how many days have passed since that date and then multiply it by the ticks per day value!

If I understand you right, you are not worried about the ticks up to a particular time of the day?! So, it would be something along the lines of :

var ticksToDate = (DateTime.UtcNow - DateTime.MinValue).Days * 864000000000;

Does that answer your question??

Upvotes: 2

ChrisWue
ChrisWue

Reputation: 19020

That is going to be rather difficult unless you have some other way of getting the current date and time. According to MSDN:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

Now, if you know the current date and time, you can calculate how many days have passed since January 1, 0001 and use that to calculate the number of ticks.

Upvotes: 0

Related Questions