简体   繁体   中英

Truncate/Convert Datacolumn Dates. C# Console App

I have ODBC DSN connection that I bring into DataTable. Column Three is populated with TIMESTAMP data.

I have...

BRANCH----| TYPE-----| ID

1---------------| R-----------| 14/03/2013 9:42
1---------------| R-----------| 9/01/2015 9:42
3---------------| W-----------| 13/09/2014 9:42
2---------------| R-----------| 1/03/2012 9:42

I want to see:

1---------------| R-----------| 03/2013
1---------------| R-----------| 01/2015
3---------------| W-----------| 09/2014
2---------------| R-----------| 03/2012

I have used the code below before to truncate data. However I cant make it work in this case due to type mismatch.

Any pointers would be great.

dcUnits.Expression = string.Format
  ("SUBSTRING({0}, 1, 1)+''+{1}+''+{2}", "BRANCH", "TYPE", "ID");

You can apply YearMonth standard format specifier to the column. The "Y" or "y" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.YearMonthPattern property of a specified culture. For example, the custom format string for the invariant culture is "yyyy MMMM".

This is just an idea which might work for you.

DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
DateTime myDT = new DateTime(ID);
dcUnits.Expression = string.Format
("SUBSTRING({0}, 1, 1)+''+{1}+''+{2}", "BRANCH", "TYPE", myDT.ToString("y", myDTFI));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM