c# - Convert DateTime Column Format in DataSet from US/UK to US -
how can optimize below method convert dataset datetime column us/uk format format. initial date format can us/uk or other country. need convert date format in output dataset
private dataset modifydatetousformat(dataset ds) { dataset dsres = new dataset(); datetimeformatinfo usdtfi = new cultureinfo("en-us", false).datetimeformat; //convert datetime string type datatable dtcloned = ds.tables[0].clone(); foreach (datacolumn dc in ds.tables[0].columns) { if (dc.datatype == typeof(datetime)) dtcloned.columns[dc.columnname].datatype = typeof(string); } foreach (datarow row in ds.tables[0].rows) dtcloned.importrow(row); //change string format format. since database expects format only. foreach (datarow row in dtcloned.rows) { foreach (datacolumn dc in ds.tables[0].columns) { if (dc.datatype == typeof(datetime)) row[dc.columnname] = datetime.parse(row[dc.columnname].tostring(), cultureinfo.currentculture, datetimestyles.nocurrentdatedefault).tostring(usdtfi.shortdatepattern); } } dsres.tables.add(dtcloned); return dsres; }
you creating problem attempting solve.
a datetime
not carry any culture info or particular format. neither date , time data types in database, such datetime
in sql server.
when convert datetime
string
- that's when you're using culture info apply format. trying use different culture parse string in different format , parse datetime
, again has no culture info.
the simple answer - don't this.
to make clear - when see output database in either sql management studio, visual studio debugger, or whatever environment using - that's not datetime
. that's string
representation of datetime
. wherever looking apply current culture when view it. doesn't exist in database in format.
for example, in sql server, datetime
field stored 8 bytes. consider:
-- implicitly convert varchar using current culture info select getdate() -- outputs: 2013-08-19 15:38:41.503 -- explicity convert varchar using current culture info select convert(varchar, getdate()) -- outputs: aug 19 2013 3:38pm -- explicity convert varbinary show how it's stored select convert(varbinary, getdate()) -- outputs: 0x0000a21f0101d1c3
Comments
Post a Comment