Tag Archives: timevalue
C# || How To Get The Date Value & Time Value From A Date Using C#
The following is a module with functions which demonstrates how to get the date value and time value from a DateTime using C#.
The functions listed on this page demonstrates how to isolate the date portion of a date, as well as the time portion of a date. For example, given the date “5/23/2019 7:28:31 PM”, the date value would be 5/23/2019, and time value 7:28:31 PM.
1. Date Value
The example below demonstrates the use of ‘Utils.Methods.DateValue‘ to get the date value of a date.
The DateTime object value contains the date information, with the time portion set to midnight (00:00:00)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Date Value // Declare date value var original = "5/23/2019 7:28:31 PM"; // Get the date portion var dateOnly = Utils.Methods.DateValue(original); // Display results Console.WriteLine($@" Original: {original} Date Value: {dateOnly} "); // example output: /* Original: 5/23/2019 7:28:31 PM Date Value: 5/23/2019 12:00:00 AM */ |
2. Time Value
The example below demonstrates the use of ‘Utils.Methods.TimeValue‘ to get the date value of a date.
The DateTime object contains the time information, with the date portion set to January 1 of the year 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Time Value // Declare date value var original = "5/23/2019 7:28:31 PM"; // Get the time portion var timeOnly = Utils.Methods.TimeValue(original); // Display results Console.WriteLine($@" Original: {original} Time Value: {timeOnly} "); // example output: /* Original: 5/23/2019 7:28:31 PM Time Value: 1/1/0001 7:28:31 PM */ |
3. 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 23, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Methods { /// <summary> /// Returns a DateTime value containing the date information, with /// the time information set to midnight (00:00:00) /// </summary> /// <param name="value">A string expression representing a date/time /// value from 00:00:00 on January 1 of the year 1 through 23:59:59 /// on December 31, 9999</param> /// <returns>A DateTime value containing the date information, with /// the time information set to midnight (00:00:00)</returns> public static DateTime DateValue(string value) { return DateValue(System.Convert.ToDateTime(value)); } /// <summary> /// Returns a DateTime value containing the date information, with the /// time information set to midnight (00:00:00) /// </summary> /// <param name="value">A DateTime value</param> /// <returns>A DateTime value containing the date information, with the /// time information set to midnight (00:00:00)</returns> public static DateTime DateValue(DateTime value) { return value.Date; } /// <summary> /// Returns a DateTime value containing the time information, with the /// date information set to January 1 of the year 1 /// </summary> /// <param name="value">A string expression representing a date/time /// value from 00:00:00 on January 1 of the year 1 through 23:59:59 /// on December 31, 9999</param> /// <returns>A DateTime value containing the time information, with the /// date information set to January 1 of the year 1</returns> public static DateTime TimeValue(string value) { return TimeValue(System.Convert.ToDateTime(value)); } /// <summary> /// Returns a DateTime value containing the time information, with /// the date information set to January 1 of the year 1 /// </summary> /// <param name="value">A DateTime value</param> /// <returns>A DateTime value containing the time information, with /// the date information set to January 1 of the year 1</returns> public static DateTime TimeValue(DateTime value) { return (new DateTime()).Add(value.TimeOfDay); } } }// http://programmingnotes.org/ |
4. 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 37 38 39 40 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 23, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; public class Program { static void Main(string[] args) { try { // Declare date value var original = "5/23/2019 7:28:31 PM"; // Get the date portion var dateOnly = Utils.Methods.DateValue(original); // Get the time portion var timeOnly = Utils.Methods.TimeValue(original); // Display results Display($@" Original: {original} Date Value: {dateOnly} Time Value: {timeOnly} "); } 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.