How do I convert a 7-digit Julian Date (2016092) to a regular Calendar date (MM-DD-YYYY)?
I was thinking of taking the last three digits and converting it to a regular date then appending the first four digits as the year but I'd have to consider leap years.
Expected output: 04-01-2016
My current (SQL) code which solves the problem is
DECLARE #dt char(7)
SET #dt = 2016092
SELECT DATEADD(dd, CAST(RIGHT(#dt, 3) AS int) - 1, CAST(LEFT(#dt, 4) AS datetime))
How can I implement it on C#?
You don't have any kind of Julian Day (Date) format which is
https://en.wikipedia.org/wiki/Julian_day
But a kind of custom format which can be reconstructed from the sql provided:
year * 1000 + days from 1st Jan + 1
So 2016092 means year 2016 and 092 - 1 = 91st day from the 1st of Jan (1st of Apr)
Implementation:
int source = 2016092;
DateTime result = new DateTime(source / 1000, 1, 1).AddDays(source % 1000 - 1);
Console.WriteLine($"{result:MM-dd-yyyy}");
Outcome:
04-01-2016
Related
I have DateTimeOffset input param. Need to create other DateTimeOffset param which Month is 2 month less than from input:
//DateTimeOffset input;
DateTimeOffset modified = new DateTimeOffset(input.Year,
input.Month - 2, input.Day,
input.Hour, input.Minute,
input.Second, input.Millisecond,
input.Offset);
I get an exception
Year, Month, and Day parameters describe an un-representable DateTime.
What is wrong? - the month is 4. S0 4-2 = 2 is valid
Thanks
I think the month is a red herring, it's probably more likely your day is above 28 which means you are trying to create a date like 30th Feb (which doesn't exist!).
Use the AddMonths method instead
DateTimeOffset modified = input.AddMonths(-2);
I am developing a c#.net solution with a Store Procedure in oracle that calculates End Date based on provided Start date and Duration with weekends.
i.e. Start Date: 01/3/2013 Forward days: 10
Start Date = (05/5/2011) - 10 days (includes weekends)
Start Date = 06/02/2013
thank you,
Use of AddDays method of DateTime
DateTime startDate = new DateTime(2013,3,1);
var endDate = startDate.AddDays(10);
You can do this a bunch of ways in Oracle. I'd suggest the INTERVAL way because it's standard-ish:
SELECT yourdate + INTERVAL '10' DAY, ...
The single quote around the 10 is required.
The "old" way is to just add 10 to the date; Oracle treats this as days:
SELECT yourdate + 10, ...
//DateTime start_date = ...
DateTime end_date = start_date.AddDays(10);
I previously asked this question to take a oollection of datetime objects and group them by dayOfweek and time
So just to recap: I take a collection of DateTime
List<DateTime> collectionOfDateTime = GetDateColletion();
and then grouping by dayofWeek and time of day by doing this
var byDayOfWeek = collectionOfDateTime.GroupBy(dt => dt.DayOfWeek + "-" + dt.Hour + "-" + dt.Minute);
So at this point, I have these grouped by week (consistent time) working perfectly.
I now have a new requirement to group by Month instead of by week. When i say "month", its not the same day of the month but something like "the first tuesday of each Month"
I am trying to figure out what "key" to use in a group by to group all items that fit that monthly logic (the first tuesday of the month, the second friday of each month, etc)
As an example lets say i had these dates to start out with;
var date1 = new DateTime(2013, 1, 4).AddHours(8); // This is the first Friday in Jan
var date2 = new DateTime(2013, 2, 1).AddHours(8); // This is the first Friday in Feb
var date3 = new DateTime(2013, 1, 5).AddHours(3); // This is the first Sat in Jan
var date4 = new DateTime(2013, 2, 2).AddHours(3); // This is the first Sat in Feb
var date5 = new DateTime(2013, 2, 2).AddHours(6); // This is the first Sat in Feb - different time
If these were the dates that went into the original array, i need a groupby to end up with 3 groups.
The first group would have date1 & date2 in it
The second group would have date3 and date4 in it.
date5 would be on its own as it doesn't match any of the other groups given the different time
Can anyone suggest anyway to group by that criteria?
I think it's easier than it looks:
var byDayOfMonth = from d in dates
let h = (d.Day / 7) + 1
group d by new { d.DayOfWeek, h } into g
select g;
Local variable h = (d.Day / 7) + 1 sets which DayOfWeek within that month it actually is.
I run it for test and received 2 groups, exactly the same as in your example. Keys for that groups are:
{ DayOfWeek = Friday, h = 1 }
{ DayOfWeek = Saturday, h = 1 }
What means, there are groups for 'First Friday of month' and 'First Saturday of month'.
You can easily extend grouping key by d.Hour and/or d.Minute if you like:
var byDayOfMonth = from d in dates
let h = (d.Day / 7) + 1
group d by new { d.DayOfWeek, h, d.Hour, d.Minute } into g
select g;
Results (keys only):
{ DayOfWeek = Friday, h = 1, Hour = 8, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 3, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 6, Minute = 0 }
There is probably an easier way to do this but this is what's come to me:
I gather from your question that you need to group everything from "the first Tuesday of February until the first Monday of March" etc. such that you get these "month" spans that are a variable number of days - depending on the month in which they start. If so then you really need to break this down into ranges using the day of the year so:
Group by the First Wednesday of the Month 2013
Group 0 (0-1)
All DayOfYear between 0 and 1 2013
Group 1 (2-36)
The first Wednesday of the month: January is DayOfYear 2.
The first Wednesday of the month: February is DayOfYear 37.
etc.
So the first range is a function f such that f(32) = 1 (DayOfYear is 32) because it falls in the range 2 to 37. This f is an indexed collection of ranges, finding the item in the collection that a given DayOfYear falls into, and returning that item's index as the group number.
You can dynamically build this table by getting your min and max dates from GetDateCollection to determine the overall range. Because the logic surrounding dates is a pretty complex topic in of itself I'd fall back on a library like NodaTime (specifically the arithmetic documentation), start with the min date, advance day by day until I found the first qualifying day (i.e., "first Monday of the month") and create a range 0 to that day - 1 as group 0 and push that onto an indexed collection (ArrayList likely). Then loop from that date using LocalDate.PlusWeeks(1) until the month changes, constructing a new range and pushing that range onto the same indexed collection.
Each time you cross into a new year you'll have to add 365 (or 366 if the previous year is a leap year) to your DayOfYear as you build your indexed collection since DayOfYear resets each year.
Now you've got a collection of ranges that acts as a table that groups days into the desired units based on their DayOfYear.
Write a function that traverses the table comparing the DayOfYear (+ [365|366] * x where x is the # of years the date you are comparing is from your min year) of a given date against the items in the collection until you locate the range that day falls within, and return that index of that item as the group number. (Alternatively each range could be a Func<DateTime,bool> that returns true if the provided DateTime falls in that range.)
An alternative data structure to the collection of ranges would be an array of ushort with length equal to all the days from min to max dates in your date range, and the value for each day their assigned group number (calculated with ranges, as above). This will perform faster for grouping, though the performance may not be noticeable if you're working with a smaller dataset (only a few hundred dates).
To group by using Linq maybe this code will help you:
List<DateTime> collectionOfDateTime = GetDateColletion();
collectionOfDateTime.GroupBy(s => Convert.ToInt16(s.DayOfWeek) & s.Hour & s.Minute);
I have a table containing a field of dates + a field named WeekDayID (sun=0, mon=1, tue=2, wed=3, thu=4, fri=5, sat=6) + some other field (not important to mention), and I need two reports:
1.based on month
2.based on week
I have done the report number 1
my question is HOW CAN I ACHIEVE report 2, I need something that gives me this:
let's say we have dates from 2001/01/01 to 2001/01/31
I want to get all days grouped by week for example if 2001/01/01 is Sunday i want groups like:
1st week : 2001/01/01 - 2001/01/02 - ... - 2001/01/07
2nd week : 2001/01/08 - ... - 2001/01/14
3rd week : 2001/01/15 - ... - 2001/01/21
4th week : 2001/01/22 - ... - 2001/01/28
5th week : 2001/01/29 - 2001/01/30 - 2001/01/31
For the week number you can use the Datepart function with the value 'ww' for the first parameter:
SELECT Datepart(ww, myDate) FROM myTable
For the date range, you can do some math tricks: Substract to your date the weekday to get the first day of the week:
SELECT DateAdd(day, - DatePart(weekday, myDate), myDate) Week_Start
from myTable
here you have a link to the DatePart function:
http://msdn.microsoft.com/es-es/library/ms174420.aspx
*(This is for ISV database so I am kind of reverse engineering this and cannot change) ...
How can I do the following date to int (visa/versa) conversion in C# ...
So Say the Date is:
5/17/2012
it gets converted to int
77207
in the database.
At first I thought this was a Julian date however it does not appear to be the case. I was fooling around with the method from Julian Date Question however this does not match up.
var date = ConvertToJulian(Convert.ToDateTime("5/17/2012"));
Console.WriteLine(date);
public static long ConvertToJulian(DateTime Date)
{
int Month = Date.Month;
int Day = Date.Day;
int Year = Date.Year;
if (Month < 3)
{
Month = Month + 12;
Year = Year - 1;
}
long JulianDay = Day + (153 * Month - 457)
/ 5 + 365 * Year + (Year / 4) -
(Year / 100) + (Year / 400) + 1721119;
return JulianDay;
}
Outputs 2456055 //Should be 77207
I've been using this SQL to do the conversion:
SELECT Convert(date, CONVERT(CHAR,DATEADD(D, 77207, '1800-12-28'),101))
and it appears to be accurate. How could I do this conversion in C# ? And can someone edify me as to what standard this is based on or is it simply a random conversion. Thanks in advance.
//TO int
var date = new DateTime(1800,12,28,0,0,0);
var daysSince = (DateTime.Now-date).Days;
//FROM int
var date = new DateTime(1800, 12, 28, 0, 0, 0);
var theDate = date.AddDays(77207);
This appears to be a Clarion Date:
the number of days that have elapsed since December 28, 1800
Allegedly to, Display Clarion Dates In Excel it only takes
subtracting 36161 from the value and formatting it as a date
If it is a linear formula, you should be able to calculate formula in the form of y=mx+b. You would need a minimum of two data points.
Here is the vb.net Code I use to convert Clarion Date to Julian Date:
Dim ldblDaysToSubtract As Double = 36161.0
mclsRevEmployeeRecd.BirthDate(istrBirthDate:=(CDbl(E1Row.Item("BIRTH_DT")) - ldblDaysToSubtract).ToString)
mstrBirthDate = Format(CDate(Date.FromOADate(CDbl(istrBirthDate)).ToString), "MM/dd/yyyy")