Tag Archives: date set time
C# || How To Append & Join A Date & Time Value Together Using C#
The following is a module with functions which demonstrates how to append and join a date and time value together using C#.
1. Append Date & Time
The example below demonstrates the use of ‘Utils.Extensions.SetTime‘ to append a date and time value together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Append Date & Time using Utils var dteDate = Convert.ToDateTime("1/27/1991"); var time = Convert.ToDateTime("7:28:33 PM"); Console.WriteLine($"dteDate: {dteDate}"); // Append time to the date var result = dteDate.SetTime(time); Console.WriteLine($"Result: {result}"); // expected output: /* dteDate: 1/27/1991 12:00:00 AM Result: 1/27/1991 7:28:33 PM */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Extensions { /// <summary> /// Appends a date and time value together /// </summary> /// <param name="date">The date portion</param> /// <param name="time">The time portion</param> /// <returns>The appended date and time value</returns> public static DateTime SetTime(this DateTime date, DateTime time) { return date.Date.Add(time.TimeOfDay); } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { static void Main(string[] args) { try { var dteDate = Convert.ToDateTime("1/27/1991"); var time = Convert.ToDateTime("7:28:33 PM"); Display($"dteDate: {dteDate}"); // Append time to the date var result = dteDate.SetTime(time); Display($"Result: {result}"); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Append & Join A Date & Time Value Together Using VB.NET
The following is a module with functions which demonstrates how to append and join a date and time value together using VB.NET.
1. Append Date & Time
The example below demonstrates the use of ‘Utils.SetTime‘ to append a date and time value together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
' Append Date & Time Imports Utils Dim dteDate = DateValue("1/27/1991") Dim time = TimeValue("7:28:33 PM") Debug.Print($"dteDate: {dteDate}") ' Append time to the date Dim result = dteDate.SetTime(time) Debug.Print($"Result: {result}") ' expected output: ' dteDate: 1/27/1991 12:00:00 AM ' Result: 1/27/1991 7:28:33 PM |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 27, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Appends a date and time value together ''' </summary> ''' <param name="[date]">The date portion</param> ''' <param name="time">The time portion</param> ''' <returns>The appended date and time value</returns> <Runtime.CompilerServices.Extension()> Public Function SetTime([date] As Date, time As Date) As Date Return [date].Date.Add(time.TimeOfDay) End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 27, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Public Module Program Sub Main(args As String()) Try Dim dteDate = DateValue("1/27/1991") Dim time = TimeValue("7:28:33 PM") Display($"dteDate: {dteDate}") ' Append time to the date Dim result = dteDate.SetTime(time) Display($"Result: {result}") Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.