Francis Saul
Francis Saul

Reputation: 750

how can I format this string to MM/dd/yyyy

I have tried a lot of syntax but I can't get it work,

from my DataTable i have this values , please see below

enter image description here

the above values are from the jQuery datepicker, I just make them values in my query.

C#: I catch the values from the datepicker using this parameter`

    List<DateTime> WeekDates

how can i make it to display as 1/6/2016 . thank you for any help!

      foreach (DataRow row in dt.Rows)
        {
            Sale sales = new Sales();
            sale.WeekStart = row["WeekStart"].ToString("MM/dd/yyyy");
        }

UPDATE is my date values are string or a DateTime already? I catch the values in my webmethod using this

List<DateTime> WeekDates, but when it comes to my dataTable it has a single quotes , notice it.

Upvotes: 0

Views: 1513

Answers (9)

Francis Saul
Francis Saul

Reputation: 750

Thank you for all who answered my question. As for now the quick solution for me is to remove unnecessary string to display the format MM/dd/yyyy using this syntax

row["WeekStart"].ToString().Remove(9); 

Most of you answered the correct syntax to convert it to the MM/dd/yyyy but I figured out that the values of dates from my jQuery datepicker has a single quote value, what I did is removed it before feeding to my SQL parameters and everything works fine. I already used that syntax before I post here , I just need to remove the single quote in my datetime values.

Upvotes: 0

M.Owais
M.Owais

Reputation: 81

[date].ToString("MM/dd/yyyy"); 

you can use this one

Upvotes: 2

Syed Jamil Uddin
Syed Jamil Uddin

Reputation: 267

Try

foreach (DataRow row in dt.Rows)
{
    Sale sales = new Sales();
    var date = Convert.ToDateTime(row["WeekStart"]);
    sale.WeekStart = date.ToString("MM/dd/yyyy"); // If sale.WeekStart field is string
}

Complete description: http://www.csharp-examples.net/string-format-datetime/

Upvotes: 3

Angelo
Angelo

Reputation: 41

try this

string text = row["Weekstart"].ToString();
DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy", null);

Upvotes: 1

user2148124
user2148124

Reputation: 970

Try something like this

 string date = "1/6/2016 12:00:00 AM";
 string d = Convert.ToDateTime(date).ToShortDateString();

O/P : 1/6/2016

In your case perhaps following code will be useful

 DataTable dt = new DataTable();
 dt.Columns.Add("Date");
 DataRow row1 = dt.NewRow();
 row1["Date"] = "1/6/2016 12:00:00 AM";
 dt.Rows.Add(row1);
 DataRow row2 = dt.NewRow();
 row2["Date"] = "2/6/2016 12:00:00 AM";
 dt.Rows.Add(row2);

 List<string> WeekStart = new List<string>();

 foreach (DataRow row in dt.Rows)
 {
     WeekStart.Add(Convert.ToDateTime(row["Date"]).ToShortDateString());
 }

Upvotes: 1

Tony
Tony

Reputation: 129

If those values came from a database, you may want to format them first before populating to your DataTable. For your desired format, you may refer to this post.

Update 1 Try formatting your jQuery DatePicker to something like:

$(".selector").datepicker({ dateFormat: "MM/dd/yyyy" });

Update 2

foreach (DataRow row in dt.Rows)
{
    Sale sales = new Sales();
    sale.WeekStart = Convert.ToDateTime(row["WeekStart"]).ToString("MM/dd/yyyy");
}

Upvotes: 1

Imad
Imad

Reputation: 7490

Try

(DateTime.ParseExact(row["WeekStart"]), "M/d/YYYY hh:mm:SS tt", System.Globalization.CultureInfo.InvariantCulture)).ToString("MM/dd/yyyy");

Upvotes: 1

Faraz Ahmed
Faraz Ahmed

Reputation: 1607

Have you tried

sale.WeekStart =DateTime.Parse(row["WeekStart"].ToString()).ToString("MM/dd/yyyy");

I think this is work for you

Upvotes: 1

Marco Bong
Marco Bong

Reputation: 690

[YourDateTimeVarialble].ToString("MM/dd/yyyy");

Upvotes: 2

Related Questions