Reputation: 1046
I am currently doing this way to convert week of the year to milliseconds.
DateTime source = new DateTime(2014, 1, 1);
int weeks = 14; //first week of April
**int month = 4; //I don't use it yet**
int delta = 7 + DayOfWeek.Wednesday - source.DayOfWeek;
if (delta >= 7)
delta -= 7;
source = source.AddDays((weeks- 1) * 7 + delta);
double timeMs = (source - new DateTime(1970, 1, 1)).TotalMilliseconds;
Although, I came across with problems that a given week can belong to two months, just like the 14th week of 2014 belongs to March and April.
For example, I receive these entries:
week: 14, month: 3, year: 2014, total: 13
week: 14, month: 4, year: 2014, total: 98
The way I'm doing I get the same time-stamp in ms for both entries which is good for building a chart of week evolution but it is bad if I want to filter data by month and week at the same time (example: from week 14 of April to week 15 of April -> I don't want to get those 13 equipment to be related to the week 14 of April)
So my question is: given a week of the year, month and year is there any way (library or not) to compare two dates accurately with these fields?
PS: On the front-end I use moment.js to get the week of the year, month and year of a given date.
Thanks anyway.
Upvotes: 1
Views: 1500
Reputation:
You can use the Week class of the Time Period Library for .NET:
// ----------------------------------------------------------------------
public void WeekOfYear()
{
Week week = new Week( 2014, 14 );
Console.WriteLine( "Week: {0}", week );
Console.WriteLine( "Year: {0}, Month: {1}", week.Start.Year, week.Start.Month );
Console.WriteLine( "NextWeek: {0}", week.GetNextWeek() );
} // WeekOfYear
Upvotes: 1
Reputation: 421
You can use the calendar class to get the week number http://msdn.microsoft.com/en-us/library/system.globalization.calendar(v=vs.110).aspx
Upvotes: 0