* 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.
Blog Archive
-
▼
2009
(48)
-
▼
August
(12)
- "PrintForm" Component, available in .NET 3.5 SP1
- How to see Print Preview & Print a WindowsForm
- Deployment of a Windows Forms / Console Application
- Load Text File Rtb
- Load Crystal Report from File
- Export Data from Listview to Excel
- Load Data from Excel File to DataGridView
- DateTime Operations How to get the components of d...
- DateTime Value Manipulation
- How can Reading Excel into Access Database Using A...
- Run Console App From Windows Form Button
- How to Create a File or Folder in C#?
-
▼
August
(12)
My Links
- C# Snippets: 1
- C# Snippets: 2
- XMLFOX - CSHARP SAMPLES
- Q: Export your Crystal Report data to PDF
- Q: To create a Excel report wihtout using Excel object model Ex
- Q: Using C#, How to Save,Update Data to and from XML with out using the sql server database
- Q: upload excel to SQL using C#.net,VS2005
- Q: Export DataTable to Excel using c# with Formatting Styles
- Q: Crystal report in c# .Net
- Q: Import excel into Sql server 2005 Table
- Q: READ, UPDATE & DELETE XML FILE USING X-PATH
- Q: Export Data from Listview to Excel
- Q: About GridView Data To Excel C#
- Q: Simple way to convert excel sheet to sql table
- Q: How to send data from c# file(.cs) to xslt file
- Q: How to Export and import data from sqlserver to excel sheet in vb.net
- Q: Migration of data from access to excel and vice versa
- Q: How to insert the data from excel to Database in .Net?
- Q: How can we create excel sheet in c# using data grid
- Q: DateTime Value Manipulation
- Q: create an array from a txt file
- Q: HowtoOepnandReadanExcelSpreadsheetinaListViewin.NET
- Q: How to fill data to excel
- Q: Reading Image from Excel file using C# .Net
- C# / CSharp Tutorial
- stackoverflow.com/questions
- General .NET Projects
- Sample .NET Projects with source code
- CSharpArticles
- PHPManual
- phpmanual/en/install.windows
- ProtoPage
Tuesday, August 11, 2009
DateTime Operations How to get the components of date & time (day, month, year, minute, hour, sec, millisec) using “DateTime” Class in C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment