using System;
public class MainClass{
/// <summary>
/// Gets the days in year.
/// </summary>
/// <param name="date">The date.</param>
/// <returns></returns>
public static int GetDaysInYear(DateTime date)
{
if (date.Equals(DateTime.MinValue))
{
return -1;
}
DateTime thisYear = new DateTime(date.Year, 1, 1);
DateTime nextYear = new DateTime(date.Year + 1, 1, 1);
return (nextYear - thisYear).Days;
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Gets the days in month.
using System;
public class MainClass{
/// <summary>
/// Gets the days in month.
/// </summary>
/// <param name="date">The date.</param>
/// <returns></returns>
public static int GetDaysInMonth(DateTime date)
{
return DateTime.DaysInMonth(date.Year, date.Month);
}
}
Gets the days between.
using System;
public class MainClass{
/// <summary>
/// Gets the days between.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="last">The last.</param>
/// <param name="inclusive">if set to <c>true</c> [inclusive].</param>
/// <returns></returns>
public static int GetDaysBetween(DateTime first, DateTime last, bool inclusive)
{
TimeSpan span = last - first;
return inclusive ? span.Days + 1 : span.Days;
}
}
Contains helper for converting to and from the date formats
//http://facebooktoolkit.codeplex.com/
//http://facebooktoolkit.codeplex.com/license
using System;
namespace Facebook.Utility
{
///<summary>
/// Contains helper for converting to and from the date formats provided by facebook
///</summary>
public static class DateHelper
{
///<summary>
/// Returns a datetime corresponding to 1/1/1970
///</summary>
public static DateTime BaseUTCDateTime
{
get { return new DateTime(1970, 1, 1, 0, 0, 0); }
}
///<summary>
/// Event dates are stored by assuming the time which the user input was in Pacific
/// time (PST or PDT, depending on the date), converting that to UTC, and then
/// converting that to Unix epoch time. This algorithm reverses that process.
///</summary>
public static DateTime ConvertUnixTimeToDateTime(long secondsSinceEpoch)
{
DateTime utcDateTime = BaseUTCDateTime.AddSeconds(secondsSinceEpoch);
int pacificZoneOffset = utcDateTime.IsDaylightSavingTime() ? -7 : -8;
return utcDateTime.AddHours(pacificZoneOffset);
}
/// <summary>
/// Convert datetime to UTC time, as understood by Facebook.
/// </summary>
/// <param name="dateToConvert">The date that we need to pass to the api.</param>
/// <returns>The number of seconds since Jan 1, 1970.</returns>
public static long ConvertDateToFacebookDate(DateTime dateToConvert)
{
return (long)((dateToConvert - BaseUTCDateTime).TotalSeconds);
}
/// <summary>
/// Convert UTC time, as returned by Facebook, to localtime.
/// </summary>
/// <param name="secondsSinceEpoch">The number of seconds since Jan 1, 1970.</param>
/// <returns>Local time.</returns>
internal static DateTime ConvertDoubleToDate(double secondsSinceEpoch)
{
#if !SILVERLIGHT
return TimeZone.CurrentTimeZone.ToLocalTime(BaseUTCDateTime.AddSeconds(secondsSinceEpoch));
#else
return TimeZoneInfo.ConvertTime(BaseUTCDateTime.AddSeconds(secondsSinceEpoch), TimeZoneInfo.Local);
#endif
}
//Event dates are stored by assuming the time which the user input was in Pacific
// time (PST or PDT, depending on the date), converting that to UTC, and then
// converting that to Unix epoch time. This algorithm reverses that process.
internal static DateTime ConvertDoubleToEventDate(double secondsSinceEpoch)
{
DateTime utcDateTime = BaseUTCDateTime.AddSeconds(secondsSinceEpoch);
int pacificZoneOffset = utcDateTime.IsDaylightSavingTime() ? -7 : -8;
return utcDateTime.AddHours(pacificZoneOffset);
}
/// <summary>
/// Convert datetime to UTC time, as understood by Facebook.
/// </summary>
/// <param name="dateToConvert">The date that we need to pass to the api.</param>
/// <returns>The number of seconds since Jan 1, 1970.</returns>
internal static double? ConvertDateToDouble(DateTime? dateToConvert)
{
return dateToConvert != null ? new double?((dateToConvert.Value - BaseUTCDateTime).TotalSeconds) : null;
}
}
}
Short time date format
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("(t) Short time: . . . . . . . {0:t}
",DateTime.Now);
}
}
Long time date format
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("(T) Long time:. . . . . . . . {0:T}
",DateTime.Now);
}
}
Full date short time format
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("(f) Full date/short time: . . {0:f}
",DateTime.Now);
}
}