Reputation: 23
In string array I have date field, which I want to format for date only and bind it to date time object.
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM"
};
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
DateTime dt = DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null);
Am not getting only date. still am getting date and time.
Upvotes: 0
Views: 131
Reputation: 7352
Use like this
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM" };
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
string dt= DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("dd/MM/yyyy");
in this way you will get date as string, but if you want only date part as DateTime
type then it isn't possible because in DateTime
type if you don't specify any time then it will automatically add a default time as 12:00:00 AM(00:00:00)
in DateTime
Update
you can use like this for all culture
string dt = DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
or you can use simply like this
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM" };
string dateFromString = Convert.ToDateTime(strArray[4]).ToString("dd/MM/yyyy");
Upvotes: 0
Reputation: 2824
You can take only the Date
without Time
from a DateTime
object in this way:
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
var onlyDate = dateFromString.Date;
// Display date using short date string.
Console.WriteLine(onlyDate .ToString("d"));
Upvotes: 1