Skip to main content

Posts

Showing posts with the label datetime

c# - How to get the month name from month number

Month name from number The following asp.net c# example code demonstrate us how can we get month name from number. this examplecode also demonstrate us how can we get month name from a datetime object. bellow is the explain of example source code. We created a datetime object using DateTime.Today property. DateTime.Today property get the current date without time in the web server.DteTime.Month property gets the month number from a datetime object. so we uses the DateTime.Month property to get the month number fromtoday date that represent the current month of web server. At last we call the DateTimeFormatInfo.GetMonthName() method that returns the culture specific full name of the specified month based on theculture associated with the current DateTimeFormatInfo object. GetmonthName method require an integer type argument that represent month number. CultureInfo.DateTimeFormat property get or set a DateTimeFormatInfo that defines the cult...

c# - How to add weeks to a DateTime object

DateTime add weeks The following asp.net c# example code demonstrate us how can we add one or more weeks with a DateTimeobject programmatically at run time in an asp.net application. By default, .net framework's DateTime Class have nomethod or property to add week with a DateTime object. So we need to go technically to add weeks with a date. We know that one week is equal to 7 days, two weeks is equal to 14 days, three weeks is equal to 21 daysand so on. .Net framework's DateTime Class DateTime.AddDays() method allow us to add specified days with a DateTimeobject. This method require to pass a parameter which hold number of days to add. So, we can use this AddDays()method to add weeks with a DateTime object. As example, if we want to add one week with a date then we can call the method as DateTime.AddDays(7) and if we want toadd three weeks with a DateTime object then we can call the method as DateTime.AddDays(21) and so on. We just ne...

c# - How to get the number of days in a given month

Get days in month DateTime.DaysInMonth() method return the number of days in the specified month and year. this method exists in System namespace.DateTime.DaysInMonth(year, month) method require to pass two parameters named 'year' and 'month'. the 'year' parameter valuedata type is System.Int32 which represent the specified year. the 'month' parameter value data type also System.int32 which representa month number ranging from 1 to 12. This method return value data type is System.Int32 which represent the number of days in specified month for the specified year. this is very useful method to get the number of days in February month in a specified year which depend on leap year. The following asp.net c# example code demonstrate us how can we get days (count number of days) in a specified month and year programmaticallyat run time in an asp.net application. DaysInMonth.aspx <%@ Page Language="C#" ...

c# - How to subtract two DateTimes

Subtract two DateTimes The following ASP.NET C# example code demonstrates to us how can we subtract two DateTime objects programmatically at run time in an asp.net application. The .NET framework's DateTime Subtract() method allows us to subtract the specified time or duration from this instance. DateTime Subtract() method is overloaded, those are Subtract(DateTime) and Subtract(TimeSpan). DateTime Subtract(DateTime) overloaded method subtracts the specified date and time from the instance. We need to pass a DateTime object to this method as a parameter. This method returns a TimeSpan value. The return TimeSpan object represents a time interval that is equal to the date and time of the instance minus the date and time of the parameter. DateTime Subtract(TimeSpan) overloaded method subtracts the specified duration from this instance. This method is required passing a TimeSpan object as the parameter. The TimeSpan represents the time interva...

c# - How to get days between two DateTimes

Days between two DateTimes The following asp.net c# example code demonstrate us how can we get total days difference between two DateTime objectsprogrammatically at run time in an asp.net application. DateTime.Subtract(DateTime) method allow us to subtract the specified date and time from this instance. We just need to passa DateTime object as parameter to subtract from specified DateTime object. DateTime.Subtract(DateTime) method return aSystem.TimeSpan type value. This TimeSpan value represents a time interval that is equal to the date and time of instance minusdate and time of parameter. TimeSpan.TotalDays property get the value of the current TimeSpan structure expressed in whole and fractional days. TimeSpan.Days property get the days component of the time interval represented by the current TimeSpan structure. This property doesnot return any fractional day, it just return full day part from a TimeSpan object. Result can be negative ...

c# - How to get hours between two DateTimes

Hours between two DateTimes The following asp.net c# example code demonstrate us how can we get number of hours between two DateTimeobjects programmatically at run time in an asp.net application. .Net framework's DateTime Class DateTime.Subtract(DateTime) overloaded method allow us to subtract a specifieddate and time from this instance. DateTime.Subtract() method need to pass a DateTime object to subtract from specifiedDateTime object. This method return a System.TimeSpan value. This TimeSpan represent a time interval that is equalto the date and time represented by this instance minus the date and time passed by parameter. TimeSpan.TotalHours property allow us to get the value of the current TimeSpan structure expressed in wholeand fractional hours. TimeSpan.Hours property allow us to get the hours component of the time interval represented by the currentTimeSpan structure. This property return value type is System.Int32. ...

c# - How to add milliseconds to DateTime

DateTime AddMilliseconds() Method The following asp.net c# example code demonstrate us how can we add specified number of Milliseconds witha DateTime object programmatically at run time. .Net framework's DateTime Class DateTime.AddMilliseconds() methodreturn a new DateTime object that add the specified number of milliseconds to the value of this instance. DateTime.AddMilliseconds() method require to pass a parameter named 'value'. The 'value' parameter data type isSystem.Double which represent a number of whole and fractional milliseconds. This parameter value can be negative orpositive. So, we can technically add or subtract number of milliseconds from a DateTime instance. This parameter valueis rounded to the nearest integer. DateTime.AddMilliseconds() method return value data type is System.DateTime. This return DateTime value is sum ofthe date and time represented by this instance and the number of milliseconds passed by ...

c# - How to add years to DateTime

DateTime AddYears() Method The following asp.net c# example code demonstrate us how can we add one or more years with aDateTime object programmatically at run time in an asp.net application. .Net framework's DateTime Classhas a built in method to add years with a date. DateTime.AddYears() method return a new DateTime object that add the specified number of years to the valueof this instance. This method require to pass a parameter named 'value'. The 'value' parameter data type isSystem.Int32. This integer value represent a number of years to add with specified DateTime object. This isinteresting that this parameter can be negative or positive. So, technically we can subtract years from a DateTime object. DateTime.AddYears() method return value data type is System.DateTime. The return value represent a DateTime that is the sum ofthe date and time of this instance and the number of years pass by parameter. DateTime.AddYea...

c# - How to add months to DateTime

DateTime AddMonths() Method The following asp.net c# example code demonstrate us how can we add specified months with a DateTime objectprogrammatically at run time in an asp.net application. .Net framework's has a built in method DateTime.AddMonths()to add one or more months with a DateTime instance. DateTime.AddMonths() method return a new DateTime object that add the specified number of months to the valueof this instance. This method require to pass a parameter named 'months'. The 'months' parameter value data typeis System.Int32. This integer value represent a number of months to add with a date. This parameter value can be negative orpositive. So, we can technically add or subtract/remove months from a date. DateTime.AddMonths() method return a value which data type is System.DateTime. This return value is the sum of the date andtime represented by this instance and number of months passed by parameter. DateTime....

c# - How to add hours to DateTime

DateTime AddHours() Method The following ASP.NET C# example code demonstrates to us how can we add one or more hours to a DateTime object programmatically at run time. The .Net framework's DateTime AddHours() method returns a new DateTime object that adds the specified number of hours to the value of this instance. DateTime AddHours() method has a required parameter. This parameter name is value and its data type is a Double. This Double value represents the number of whole and fractional hours. This parameter value can be negative or positive. So, we can also subtract hours from a DateTime object if we pass a negative value as the parameter. DateTime AddHours() method returns a DateTime object. The return DateTime object is the sum of the DateTime instance and the number of hours passed by the parameter. The fractional part of the value parameter value is the fractional part of an hour. Such as the Parameter value 5.5 is equivalent to 5 hou...

c# - How to add minutes to DateTime

DateTime AddMinutes() Method The following ASp.NET C# example code demonstrates to us how can we add minutes to a DateTime object programmatically at run time. The .Net framework's DateTime AddMinutes() method allows us to add one or more minutes with a DateTime object. DateTime AddMinutes() method exists in the System namespace. AddMinutes() method has a required parameter named value. This value parameter data type is a Double which represents a number of whole and fractional minutes. This is very useful in that the value parameter value can be negative or positive. So, technically we can also subtract minutes from a DateTime object. DateTime AddMinutes() method returns value data type is System.DateTime. This return value is a DateTime object which is the sum of specified minutes passed by the parameter and specified DateTime object. AddMinutes() method does not modify/change the provided DateTime object instead it returns a new DateTime ...

c# - How to add days to DateTime

Add days to a DateTime object In the .NET framework, there are many built-in methods and properties are exists to manage DateTime objects. DateTime AddDays() method returns a new DateTime that adds the specified number of days to the value of this DateTime object instance. This method requires passing a parameter. This parameter name is 'value' and its data type is Double. This value parameter passes a number of whole and fractional days. This value parameter can be a negative or positive number. DateTime AddDays() method returns a System.DateTime type value. Return DateTime object is the sum of provided DateTime instance and the number of days passes by the parameter. This method does not modify the provided DateTime object instead it returns the result as a new DateTime object. The following ASP.NET C# example code demonstrates to us how can we add days with a DateTime object in the .NET framework. DateTimeAddDays.asp...