Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Tuesday, July 6, 2010

C#.Net : Method to get Last date of Month & Julian Date

Hi...

We had a requirment to run an application for the last day of the month. The following method was helpful to get the date.

Sample Code:

private DateTime GetLastDayofMonth(DateTime dtDate)

{

DateTime dtToDate = dtDate;

dtToDate = dtToDate.AddMonths(1);

dtToDate = dtToDate.AddDays(-(dtToDate.Day));

return dtToDate;

}

// Later this code was helpful to convert it to julian date:

public string GetJulian_LastDayofPreviousMonth()

{

int intJulianDate = 1000 * (DateTime.Now.AddMonths(-1).Year - 1900) + GetLastDayOfMonth(DateTime.Now.AddMonths(-1)).DayOfYear;

return intJulianDate.ToString();

}

Hope it Helps

Fauzi ~4z

Monday, April 5, 2010

C# method to convert Julian to Gregorian date

Hello there,

I was in urgent need to convert Julian date to Georgian for a asp.net application which reads data from DB2.

The following code was useful.


public static string JulianToDateTime(int julianDate)

{

int RealJulian = julianDate + 1900000;

int Year = Convert.ToInt32(RealJulian.ToString().Substring(0, 4));

int DoY = Convert.ToInt32(RealJulian.ToString().Substring(4));

DateTime dtOut = new DateTime(Year, 1, 1);

return dtOut.AddDays(DoY - 1).ToShortDateString();

}

Hope It Helps

Regards
Fauzi

Wednesday, August 5, 2009

SQL: Georgian date to Julian date

Hi...

Here is the function to convert Georgian date to Julian date. Today it was useful when I was writing a application to transfer data to ERP system.


Syntax:

CREATE FUNCTION dbo.to_julian(@date datetime) RETURNS char(6) AS

BEGIN

RETURN (SELECT '1'+ RIGHT(CAST(YEAR(@date) AS CHAR(4)),2) + RIGHT('000' + CAST(DATEPART(dy, @date) AS varchar(3)),3))

END


Execute:

SELECT dbo.to_julian(getdate())




The output is 109217 for today(2009-08-05) Where 1-> 21st century, 09 -> Current Year & 217 -> Is the number of days since the start of this year.


Regards

Fauzi