Dotnet-StuffWorld

Sunday, August 23, 2009

 

How to get the components of date & time (day, month, year, minute, hour, sec, millisec) using “DateTime” Class in C#


* To get the components of the date and time, date time class provides us different properties.

Get Day:
To get day out of date first you will have to get the date after that you can get the day component out of that for this purpose “Date and Time” class provides us a property named Day. This property returns the value of day as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_Day_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Day;
string str = a.ToString();
txt_Result1.Text = str;
}
VB
Private Sub btn_Day_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Day
Dim str As String = a.ToString()
txt_Result1.Text = str
End Sub
This simple code gets the day component out of the date.


Get Month:
To get month out of date first you will have to get the date after that you can get the month component out of that for this purpose “Date and Time” class provides us a property named Month. This property returns the value of month as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_Month_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Month;
string str = a.ToString();
txt_Result2.Text = str;
}
VB
Private Sub btn_Month_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Month
Dim str As String = a.ToString()
txt_Result2.Text = str
End Sub
This simple code gets the month component out of the date.


Get Year:
To get year out of date first you will have to get the date after that you can get the year component out of that for this purpose “Date and Time” class provides us a property named Year. This property returns the value of year as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_year_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Year;
string str = a.ToString();
txt_Result3.Text = str;
}
VB
Private Sub btn_year_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Year
Dim str As String = a.ToString()
txt_Result3.Text = str
End Sub
This simple code gets the year component out of the date


Get Hour:
To get hour out of time first you will have to get the time after that you can get the hour component out of that for this purpose “Date and Time” class provides us a property named Hour. This property returns the value of hours as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_hour_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Hour;
string str = a.ToString();
txt_Result4.Text = str;
}
VB
Private Sub btn_hour_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Hour
Dim str As String = a.ToString()
txt_Result4.Text = str
End Sub
This simple code gets the hour’s component out of the time


Get Minute:
To get minute out of time first you will have to get the time after that you can get the minute component out of that for this purpose “Date and Time” class provides us a property named minute. This property returns the value of minutes as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_minute_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Minute;
string str = a.ToString();
txt_Result5.Text = str;
}
VB
Private Sub btn_minute_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Minute
Dim str As String = a.ToString()
txt_Result5.Text = str
End Sub
This simple code gets the minute’s component out of the time.


Get Second:
To get second out of time first you will have to get the time after that you can get the second’s component out of that for this purpose “Date and Time” class provides us a property named second. This property returns the value of seconds as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
Now write the following code on button click event
C#
private void btn_second_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Second;
string str = a.ToString();
txt_Result6.Text = str;
}
VB
Private Sub btn_second_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Second
Dim str As String = a.ToString()
txt_Result6.Text = str
End Sub
This simple code gets the second’s component out of the time.


Get Milliseconds:
To get millisecond out of time first you will have to get the time after that you can get the millisecond component out of that for this purpose “Date and Time” class provides us a property named millisecond. This property returns the value of milliseconds as an integer.
To demonstrate make a window application. Drag one text box and one button on the form. Press button you will get the result in text box.
C#
private void button1_Click(object sender, EventArgs e)
{
DateTime obj = new DateTime();
obj = DateTime.Now;
int a = obj.Millisecond;
string str = a.ToString();
txt_Result7.Text = str;
}
VB
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim obj As New DateTime()
obj = DateTime.Now
Dim a As Integer = obj.Millisecond
Dim str As String = a.ToString()
txt_Result7.Text = str
End Sub
This simple code gets the millisecond’s component out of the time.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
{

this.Text = "Devasp.Net Application";
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

Me.Text = "Devasp.Net Application"
End Sub


This simple article tells how we can get the components of date and time (day, month, year, minute, hour, second, millisecond) using “Date Time” Class” in C# and VB.


 

How to see Print Preview & Print a WindowsForm



using System.Drawing.Printing; //For "PrintDocument" Class

public partial class Form3 : Form
{
PrintDocument PrintDoc1 = new PrintDocument();
PrintPreviewDialog PPDlg1 = new PrintPreviewDialog();

public Form3()
{
InitializeComponent();

PPDlg1.Document = PrintDoc1;
PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
PrintDoc1.PrintPage += PDoc_PrintPage;
}

private void btn_PrintPreview_Click(object sender, EventArgs e)
{// When PrintPreview Button Clicks
PPDlg1.ShowDialog();
}

private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
//this.DrawToBitmap(bmp, this.ClientRectangle);
this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of the Exact WindowForm size as Bitmap image
e.Graphics.DrawImage(bmp, 0, 0);
}

private void btn_PrintPage_Click(object sender, EventArgs e)
{//When Print Button Clicks, Image will be show & Ready to Print
PrintDoc1.Print();
}

}

Archives

April 2009   July 2009   August 2009   September 2009   September 2010   February 2011  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]