Number/String type 2 DateTime type in C#
- Yogesh Kumar
- Jan 15, 2017
- 2 min read
Hello friend! Today I’m going to show you how to convert a number or string type value to DateTime type value. The think question I got, one day, my friend was doing work on his project and he need to store his user date to the database and also want to return this value to the TextBox. But he got an error in the format while doing this, so the question is arrived in my mind that how can we accept a user date-time as integer and display it as real date-time format. So let’s go to do it...
Requiremnets: In this tutorial, we need the Main() method with a local variable of long type to store value from the user. An object of DateTime type in Main() method to store the valid-date and to display on the Console. An another method of DateTime type (we name it as Num2Date) with four local variables (one of string type and others are int type), and an object of DateTime type.
Here we will use a function ‘Substring’ which can take some specified values from the string and return it as int type. We divide the whole process in some simple steps;
Step 1: First open the Visual Studio and create a project of Console type.
Step 2: Now in the Main() method write the following code:
Console.WriteLine(“Please enter the number date: ”);
long num = long.Parse(Console.ReadLine()); // accept user date and store in a variable
Program obj = new Program(); // creating an object of the class
DateTime dt = obj.Num2Date(num); //calling the method and store the retured value
Console.WriteLine(dt); Console.ReadLine();
Step 3: Now create anothe method of DateTime type as Num2Date name with a parameter of long type.
Step 4: Now write the following code:
// declaring some local variable string str; int day, mon, year;
str = lnum.ToString(); // converting the value in string type
// storing the specified value in the variables
day = Convert.ToInt32(str.Substring(0, 2)); // stores 2 value form index 0 mon = Convert.ToInt32(str.Substring(2, 2)); // stores 2 value from index 2 year = Convert.ToInt32(str.Substring(4, 4)); // stores 4 values from index 4
DateTime validDate = new DateTime(year, mon, day); // stores values as date-time format
return validdate; // returns valid date
The entire code is as follows:
using System; namespace Number2Date { class Program { public DateTime Num2Date(long lnum) { // declaring some local variable string str; int day, mon, year;
string str = lnum.ToString(); // converting the value in string type
day = Convert.ToInt32(str.Substring(0, 2)); mon = Convert.ToInt32(str.Substring(2, 2)); year = Convert.ToInt32(str.Substirng(4, 4));
DateTime validdate = new DateTime(year, mon, day); return validDate; }
static void Main(string[] args) { Console.WriteLine(“Enter the date (dd-mm-yyyy): ”);
long num = long.Parse(Console.ReadLine());
Program obj = new Program();
DateTime dt = obj.Num2Date(num); Console.WriteLine(dt);
Console.ReadLine(); } } }
Result:
Enter the date (dd-mm-yyyy): 16012017 16-jan-17
Comments