how can I format this string to MM/dd/yyyy - c#

I have tried a lot of syntax but I can't get it work,
from my DataTable i have this values , please see below
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.

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/

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

[date].ToString("MM/dd/yyyy");
you can use this one

Have you tried
sale.WeekStart =DateTime.Parse(row["WeekStart"].ToString()).ToString("MM/dd/yyyy");
I think this is work for you

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

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");
}

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());
}

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

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.

Related

How can I store a formatted DateTime into column of type DateTime in Database?

I'm retrieving data from the database in a dataset and printing it in a pdfdocument. One table has got a DateTime column and the value comes as "12/27/2018 12:00:00 AM". I'm trying to format that as dd/MM/yyyy but with no success since it is expecting a DateTime and formatting return a string value. Should I change the column datatype or there is a way to maintain it and format the datetime?
I recommend changing the database from string to DateTime. But if you cannot you can use something like this
DataTable dt1 = new DataTable();
DataTable dt2 = dt1.Clone();
//change columns type
dt2.Columns["Col A"].DataType = typeof(DateTime);
int colNumber = dt2.Columns.IndexOf("Col A");
foreach (DataRow row in dt1.AsEnumerable())
{
object[] rowData = row.ItemArray;
rowData[colNumber] = DateTime.Parse(row.Field<string>("Col A"));
dt2.Rows.Add(rowData);
}

How to set format datetime in new datacolumn

dt.Columns.Add(new DataColumn("BeginDate", typeof(DateTime)));
dr["BeginDate"] = Ds.Tables["tDocument"].Rows[i]["vc01"].ToString();
How do I set the datetime format to "dd/MM/yyyy"? Right now, it looks like this:
14/09/2018 00:00:00
Here is a demo:
DataTable dt = new DataTable();
dt.Columns.Add("BeginDate", typeof(string));
DataRow dr = dt.NewRow();
dr["BeginDate"] = DateTime.Now.ToString("dd/MM/yyyy");
dt.Rows.Add(dr);
foreach (DataRow item in dt.Rows)
{
Console.WriteLine(item["BeginDate"]);
}
For your case,please try following code:
dt.Columns.Add(new DataColumn("BeginDate", typeof(string)));
dr["BeginDate"] = Ds.Tables["tDocument"].Rows[i]["vc01"].ToString("dd/MM/yyyy");
If you want to sort the datetime column in a datagridview, you just need to change the column format like bellow:
dataGrid.Columns["colName"].DefaultCellStyle.Format = "dd/MM/yyyy";
If you use datagridview, then you need to format its column, not the datasource. Otherwise you won't be able to sort column by date. Datagridview will allow you to put anything if you allow it to edit data.
var row = dataTable1.NewRow();
row["BeginDate"] = DateTime.Now;
dataTable1.Rows.Add(row);
dataGridView1.Columns["beginDateDataGridViewTextBoxColumn"].DefaultCellStyle.Format = "dd/MM/yyyy";
beginDateDataGridViewTextBoxColumn is the name of the dataGridView column which was generated for "BeginDate" column.
if you use combobox, then format it using FormatString:
comboBox1.FormatString = "dd/MM/yyyy";
The / character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture is used, and in your case the current culture uses . as the date separator.
If you want to use a literal slash, place it inside single quotes:
Ds.Tables["tDocument"].Rows[i]["vc01"].ToString("dd'/'MM'/'yyyy");
Alternatively, you could specify a format provider where the date separator is /:
Ds.Tables["tDocument"].Rows[i]["vc01"].ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Try to do Like this
dt.Columns.Add(new DataColumn("BeginDate", typeof(String)));
dr["BeginDate"] = Ds.Tables["tDocument"].Rows[i]["vc01"].ToString("dd/MM/yyyy");

How to update first column of a datatable for all rows using LINQQ or any other method?

I have a data table with 4 columns datetime,id, message & status. I want to update the datetime column with a new datetime (updated datetime is in different time zone, from CST to UTC). First I was thinking to do it like I would take the datetime column and store it in a list and convert the list to utc time zone from cst.Then add the list to the same table and rearrange the columns. But this sounds stupid so is there any way to update the existing value with new values for one column of datatable.
I found the below code in one of the posts but not sure what exactly is happening.
var rowsToUpdate =
dt.AsEnumerable().Where(r => r.Field<string>("datetime") == datetime);
foreach(var row in rowsToUpdate)
{
row.SetField("datetime", utcDate);
}
The data in datatable is in CST timezone but I want that timezone to be converted to UTC timezone and update the table.
List<DateTime> lstCST = new List<DateTime>();
lstCST = datatable.AsEnumerable().Select(r=>r.Field<DateTime>("CSTDatetime")).ToList();
List<DateTime> lstUTC = new List<DateTime>();
DateTime dt = DateTime.Now;
foreach(var v in lstCST )
{
dt= TimeZoneInfo.ConvertTimeToUtc(v);
lstUTC.Add(dt);
}
Can anybody point me in the right direction on how to do this.
If you want to update all rows you don't need LINQ but TimeZoneInfo.ConvertTimeToUtc:
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
foreach (DataRow row in dataTable.Rows)
{
DateTime cstDateTime = row.Field<DateTime>("CSTDatetime");
DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(cstDateTime, cstZone);
row.SetField("CSTDatetime", utcDateTime);
}
Look at this example:
DataTable table = new DataTable();
table.Columns.AddRange(new DataColumn[]
{
new DataColumn(),
new DataColumn(),
new DataColumn()
});
table.Rows.Add(1, 2, 3);
foreach (DataRow dr in table.Rows)
{
dr[0] = 99;
}
it will update all rows of the first column and set it to 99
To explain what the snippet does:
This is a filter:
var rowsToUpdate =
dt.AsEnumerable().Where(r => r.Field<string>("datetime") == datetime);
It selects all rows where the "datetime" column has a specific value. In your case, I guess you can ignore this, you want to do this on all rows.
So instead of iterating on the filtered rows, iterate all rows.
foreach(var row in rowsToUpdate)
{
row.SetField("datetime", utcDate);
}
The latter snippet sets the "datetime" value in each row of the selection to the value of utcDate. In your case, just replace this by first getting the "old" value, convert it and then use the above line to set the converted value.
#TimSchmelter already gave a ready-to-go solution so I won't repeat it. I rather wanted to explain what was seemingly unclear to you.

Filtering DateTime Column of DataTable Causes FormatException

I have a DataTable dt bound to a DataGridView. I have been filtering the data in this grid at run-time using the standard query syntax. However, for a known DateTime column in this DataTable the query
DataRow[] rowArray = dt.Select("DOB >= #01/01/97# AND DOB <= #31/01/97#");
But this is throwing a FormatException with the message:
String was not recognized as a valid DateTime.
I have tried changing the above to
DataRow[] rowArray = dt.Select("DOB >= #01/01/1997# AND DOB <= #31/01/1997#");
But this gave the same error. From MSDN it seems I am filtering correctly. I can confirm that the DataTable column 'DOB' is indeed a DateTime type.
What is wrong with the filter statement?
Thanks for your time.
The only thing I can think about is the Locale. This seems you use GB Locale. Try this.
CultureInfo myCultureInfo = new CultureInfo("en-gb");
dt.Locale = myCultureInfo;
DataRow[] rowArray = dt.Select("DOB >= #01/01/1997# AND DOB <= #31/01/1997#");
Just pass two DateTime variables into your Select statement like this: (based on your comment regarding inputting the date in text boxes, I also added some data validation)
DateTime startDate;
DateTime endDate;
var isValidStartDate = DateTime.TryParse(txtStartDate.Text, out startDate);
var isValidEndDate = DateTime.TryParse(txtEndDate.Text, out endDate);
if (isValidStartDate && isValidEndDate)
{
var rowArray = dt.Select(
string.Format("DOB >= #{0}# AND DOB <= #{1}#", startDate, endDate));
// do something with rowArray
}
else
{
// uh-oh...
}

formatting datetime column in a DataSet

I have a dataset that is populated by reading excel file. The dataset stores the data from the excel.
The date in the dataset in in the format 2\2\2009 12:00:00 AM but i need the data format converted to 2\2\2009 .
I want to change the format of all the data in that particular column.
Here's one way:
foreach (DataRow row in yourDataTable)
{
DateTime dt = DateTime.Parse(row["Date"].ToString());
row["Date"] = dt.ToShortDateString();
}
This is assuming that the "Date" column is just a text field rather than already a DateTime field.
You can customize the output using a pattern if it is a DateTime object to the format you wanted, "2\2\2009".
string output1 = dt.ToString(#"mm\\dd\\yyyy");
string output2 = dt.ToString("mm/dd/yyyy"); //did you mean this?
A DateTime value or column has no format. It's just binary data.
Where do you want the formatted output to appear? That's where you should apply a format string. For instance, if you wanted it to appear in a column of an ASP.NET DataGrid, then you would set the DataFormatString property of the BoundColumn to "mm/dd/yyyy".
Try this. It will work
var result = from a in ds.Tables[0].AsEnumerable()
select new[] { Convert.ToDateTime(a[0]).ToString("dd/MM/yyyy") };

Categories