Get Time part from datetime C#
[C#] DateTime Format is often needed while coding in C#.net. Datetime formats are basically used for converting Date and Time into Specified String representation of DateTime.
In this post of C# Datetime Format, we will see how to get the time factor out of the DateTime. We can get Hour, Minute and Second from the DateTime. DateTime provides Properties for getting Hour, Minute and Second.
Sample code to get Hours, Minutes and Seconds from Date [C#]:
class Program
{
static void Main(string[] args)
{
DateTime date = DateTime.Now;
Console.WriteLine("Hours= " + date.Hour);
Console.WriteLine("Minutes= " + date.Minute);
Console.WriteLine("Seconds= " + date.Second);
}
}
Output of this program will show current Hours, Minutes and Seconds.
Datetime compare
C# Datetime Compare Syntax:
public static int Compare(DateTime dt1,DateTime dt2)
Sample code to compare dates using DateTime.Compare() [C#]
public class StringToDateTime
{
public static void Main()
{
DateTime dt = new DateTime(2012, 1, 16);
DateTime dt1 = new DateTime(2012, 1, 16);
int result = DateTime.Compare(dt, dt1);
if (result == 0)
Console.WriteLine("Date is same as today's date");
}
}
//Output //Date is same as today's date
Get DateTime Difference C#
In [C#] DateTime Formats, C# Datetime provides verious ways to get the difference between dates. Using C# Datetime we can get difference betwwen two dates, we can get difference between days, difference between hours, difference between Minutes, difference between seconds.
DateTime class provides subtract method to get the difference between two dates.And using System.TimeSpan we can show the difference. We will see C# code that show how to use Subtract method and System.TimeSpan to calculate difference between two dates.
[C#] Example to get difference between two dates:
using System;
namespace DateAndTimeDifferenceSample
{
class Program
{
static void Main(string[] args)
{
DateTime fromdt = new DateTime(2012, 1, 16);
DateTime todt = new DateTime(2012, 2, 17);
TimeSpan dtDiff = todt.Subtract(fromdt);
Console.WriteLine(dtDiff);
}
}
}
//Output wiil show difference in days like below //32.00:00:00
Using C# Datetime we can also get the difference in Hours, Minutes, And Seconds. Let’s see the C# example that show how to calculate difference in Hr, Mins, and in Secs.
Example to get difference for Hours, Minutes, and Seconds [C#]:
class Program
{
static void Main(string[] args)
{
DateTime fromdt = new DateTime(2012, 1, 16, 22, 01, 55);
DateTime todt = new DateTime(2012, 2, 17, 23, 15, 00);
TimeSpan dtDiff = todt.Subtract(fromdt);
Console.WriteLine("Hours" + dtDiff.Hours);
Console.WriteLine("Minutes" + dtDiff.Minutes);
Console.WriteLine("Seconds" + dtDiff.Seconds);
}
}
//OutPut //Hours= 1 //Minutes= 13 //Seconds = 5
Parse date in C#
In this post we will look how the string with date and time to be converted to DateTime. DateTime class provides Parse() method that takes string parameter that contains date and time. We can convert below kind of string to DateTime using DateTime.Parse() method: DateTime.Parse() method converts given string representation of date and time to it’s DateTime equivalent.
Sample code to convert string to DateTime using DateTime.Parse() [C#]
using System;
public class StringToDateTime
{
public static void Main()
{
string myDateTimeValue = "2012-01-16 15:02:26";
DateTime myDateTime = DateTime.Parse(myDateTimeValue);
Console.WriteLine(myDateTime);
}
}
//Output //16/01/2012 15:02:26
Add Years to date
DateTime AddYears Syntax:
public DateTime AddYears(int value);
Sample code that show how to add year in DateTime[C#] :
using System;
using System.Globalization;
public class DateTimeAddYear
{
public static void Main()
{
DateTime selectedDate = DateTime.Parse("2 Jan 2007");
selectedDate = selectedDate.AddYears(2);
Console.WriteLine(selectedDate.ToString("dd MMM yyyy"));
}
}
//Output //2 Jan 2009
Add days in date
Some times we need to add days in the date. This is needed when we want to give some days duration to perform some actions based on date.For example if we want to publish some article for two days and then hide or delete it after two days.So to add days in given date we need to use DateTime.AddDays Method
Syntax:
public DateTime AddDays(double value)
Sample code to add days in date[C#]:
using System; using System.Globalization; public class DateTimeAddYear { public static void Main() { DateTime selectedDate = DateTime.Parse("12 Jan 2012"); selectedDate = selectedDate.AddDays(2); Console.WriteLine(selectedDate.ToString("dd MMM yyyy")); } }//Output //14 Jan 2012
Get Today’s Date
Sample code to get today’s or current date [C#]
using System; using System.Globalization; public class DateTimeAddYear { public static void Main() { // Get current date. DateTime todaysDate = = DateTime.Today; //And show today's date in needed format Console.WriteLine(todaysDate.ToString("dd MMM yyyy")); } } //Output //12 Jan 2012
Get only date part from datetime
Sample code to get today’s or current date [C#]
using System; using System.Globalization; public class DateTimeAddYear { public static void Main() { DateTime selectedDate = DateTime.Parse("12 Jan 2012"); Console.WriteLine(selectedDate.Date); } } //Output //2 Jan 2009
Get full name of the day C# datetime
Sample code to show the full name of the day [C#]
using System;
using System.Globalization;
public class Example{public static void Main(){DateTime dateToFormat = new DateTime(2008, 8, 29, 19, 27, 15);
Console.WriteLine(dateToFormat.ToString("dddd"));
}}//Output
//Friday
Date in Month Day, Year Format (MMMM dd, yyyy)
Sample code to show date time in Month Day, Year format [C#]
using System;
using System.Globalization;
public class DateTimeFomrateString
{
public static void Main()
{
DateTime datetoFormat = new DateTime(2012, 1, 12);
Console.WriteLine("Today's Date is " + datetoFormat.ToString("MMMM dd, yyyy") + ".");
}
}
//Code will display the following output:
//Today's Date is June 10, 2011.
Here MMMM is format specifier that represents month as name like for 05 it will display May. dd is format specifier from day of the month with digits from 01 to 31 and yyyy is the format specifier with four digit number like 2012.
Date in MM-dd-yy format
Sample code to format date in MM-dd-yy format [C#] :
using System;
using System.Globalization;
public class DateFormatSample
{
public static void Main()
{
string dateToFormat = "10-29-11";
string strFormatPattern = "MM-dd-yy";
DateTime formattedDate;
DateTime.TryParseExact(dateToFormat, strFormatPattern, null,
DateTimeStyles.None, out formattedDate);
Console.WriteLine("Date '{0}' Formatted to {1:d}.", dateToFormat, formattedDate);
}
}
// output:
// Date '12-30-11' Formatted to 12/30/2011.
Here MM displays in format 01 to 12, dd represents Day of the month with range from 01 to 31 and yy is year from 00 to 99.