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.
do Postback in Javascript [ASP.NET]
We can use __doPostBack() to postback page from client script using JavaScript.Or we can use Page.GetPostBackEventReference() to do postback from JavaScript. When we use Page.GetPostBackEventReference() it internally get converted to __doPostBack().
Sample that shows how to do postback from JavaScript [ASP.NET, C#]:
< input type="button" id="BaseButton1" onclick="submitPage()" value="OK"/>
Now after clicking on button it will call JavaScript function that will do the postback and then in page load we will capture and perform the operation that is needed.In code behind we use Request[ "__EVENTARGUMENT"] to get the request so that we can perform some operation on that event argument.So below is JavaScript function that uses Page.GetPostBackEventReference() and do the postback from Javascript.
function submitpage()
{
// trigger postback from JavaScript
<%= this.Page.GetPostBackEventReference(this,"Delete")%>
}
Here “this” parameter in Page.GetPostBackEventReference(this, “Delete”) is the controls id and referred to as __EVENTTARGET and accessed using Request[“__EVENTTARGET”] in code behind to determine which control caused postback from JavaScript.And “Delete” is __EVENTARGUMENT and accessed using Request[ "__EVENTARGUMENT"] in code behind.
So now in code behind we can check for Request[ "__EVENTARGUMENT"] and perform some operations based on that condition like:
protected void Page_Load(object sender, EventArgs e)
{
string eventArg = Request["__EVENTARGUMENT"];
if (!String.IsNullOrEmpty(eventArg))
{
if(eventArg == "Delete")
DeleteRecords();
}
}
private void DeleteRecords()
{
//Do something
}
List to array c#
we will see the way to convert list to an array. we use .ToArray() parameter less instance method that converts list to an array. Below example shows how list get converted in to array using .ToArray() method:
Example to convert list to array [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// List to convert into Array.
List<string> lstString = new List<string>();
lstString.Add("Convert");
lstString.Add("List");
lstString.Add("TO");
lstString.Add("Array");
// use .ToArray() Method to convert list to array
string[] strArray = lstString.ToArray();
}
}