By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,275 Members | 2,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,275 IT Pros & Developers. It's quick & easy.

C# does not have " Date type " ? and can I use DateTime without the format time?

meomap0z1
P: 7
I wanna use Date type in C# but I guess it does not have this type. However, I will use DateTime instead and now I have a new problem:
Expand|Select|Wrap|Line Numbers
  1. Console.Write("Enter the birthday: \n");
  2.                 Console.Write("The year: ");
  3.                 int year = int.Parse(Console.ReadLine());
  4.                 Console.Write("The month: ");
  5.                 int month = int.Parse(Console.ReadLine());
  6.                 Console.Write("The day: ");
  7.                 int day = int.Parse(Console.ReadLine());
  8.                 DateTime birthdate = new DateTime(year, month, day);
  9.                 Student s = new Student(ID, name, birthdate, address, phone);
  10.                 DanhSachSinhVien.Add(s);
  11.  
It will show me the time 12:00:00 AM and I do not wanna show the time
- birthdate is DateTime type in another class.
Expand|Select|Wrap|Line Numbers
  1. class Student
  2.     {
  3.         private string StudentID;
  4.         private string Name;
  5.         private DateTime Birthdate;
  6.         private string Address;
  7.         private string Phone;
  8.  
  9.         etc
  10.     }
  11.  
Therefore, any solutions for this?
Thank you, guys.
Happy Luna New Year!!
4 Weeks Ago #1

✓ answered by cactusdata

A DateTime value carries no format.
Thus, for display, always specify the format that fits the purpose, for example:

Expand|Select|Wrap|Line Numbers
  1. string textDate = Student.Birthdate.ToShortDateString();

Share this Question
Share on Google+
4 Replies


P: 72
A DateTime value carries no format.
Thus, for display, always specify the format that fits the purpose, for example:

Expand|Select|Wrap|Line Numbers
  1. string textDate = Student.Birthdate.ToShortDateString();
4 Weeks Ago #2

meomap0z1
P: 7
Your meaning is :
Expand|Select|Wrap|Line Numbers
  1.  Console.Write("Enter the birthday: \n");
  2.                 Console.Write("The year: ");
  3.                 int year = int.Parse(Console.ReadLine());
  4.                 Console.Write("The month: ");
  5.                 int month = int.Parse(Console.ReadLine());
  6.                 Console.Write("The day: ");
  7.                 int day = int.Parse(Console.ReadLine());
  8.                 DateTime birthdate = new DateTime(year, month, day);
  9.       string textDate = Student.birthdate.ToShortDateString();
  10.                 Student s = new Student(ID, name, textDate, address, phone);
  11.                 ListStudentAdd(s);
  12.  
I can not call birthDate when I use Student.

I can call
Expand|Select|Wrap|Line Numbers
  1.          string textDate = birthdate.toShortDateString();
  2.  
but I can not add it to Student s because " can not convert from 'string' to System.DateTime
Now I have another solution that I will use
Expand|Select|Wrap|Line Numbers
  1. birthdate = Convert.ToDateTime(textDate);
  2.  
and it still shows me the time, I only wanna the year - the month - the day
4 Weeks Ago #3

P: 72
You are confused.

> It will show me the time 12:00:00 AM and I do not wanna show the time

Thus, I showed you how to create a text display with no time.
If you don't apply a format, a DateTime value will be displayed with a default format including time.
This has nothing to do with your original code which is correct.

Again: A DateTime value carries no format.
4 Weeks Ago #4

meomap0z1
P: 7
Now I understand. Thanks, have a nice day bro
4 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.