with dates in the format (YYYY-MM-DDT00:00:00 ), need to convert this to YYYY/MM/DD.
Please check DateTime.ToString Method and you will find it's actually very easy to use .tostring() method to convert datetime format to whichever you
want.
Check below code:
dt.ToString("yyyy/MM/dd")
This will output your dt as like 2020/01/21(YYYY/MM/DD).
If you use this data table as data source of some data control like Gridview etc then you can apply this formatting at template level/field rendering. ELSE if you need formating of data table column then you can loop through it , get the column by name or
id and update the format.
Member
58 Points
176 Posts
convert DataTable entire column to YYYY/MM/DD format without for-loop from YYYY-MM-DDT00:00:00
Jan 21, 2020 05:50 AM|neerajkumarmodi|LINK
Hi ,
I have one Datatable which has multiple columns with dates in the format (YYYY-MM-DDT00:00:00 ), need to convert this to YYYY/MM/DD.
Thanks in Advance!
Contributor
3140 Points
983 Posts
Re: convert DataTable entire column to YYYY/MM/DD format without for-loop from YYYY-MM-DDT00:00:0...
Jan 21, 2020 06:36 AM|Yang Shen|LINK
Hi neerajkumarmodi,
Please check DateTime.ToString Method and you will find it's actually very easy to use .tostring() method to convert datetime format to whichever you want.
Check below code:
This will output your dt as like 2020/01/21(YYYY/MM/DD).
Best Regard,
Yang Shen
Contributor
2096 Points
1040 Posts
Re: convert DataTable entire column to YYYY/MM/DD format without for-loop from YYYY-MM-DDT00:00:0...
Jan 21, 2020 06:44 AM|Khuram.Shahzad|LINK
Use
DateTime.Now.ToString("yyyy/MM/dd")
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?redirectedfrom=MSDN&view=netframework-4.8#System_DateTime_ToString_System_String_
Member
58 Points
176 Posts
Re: convert DataTable entire column to YYYY/MM/DD format without for-loop from YYYY-MM-DDT00:00:0...
Jan 21, 2020 06:56 AM|neerajkumarmodi|LINK
Thanks Yang for your reply!
Its not a single string, complete datatable column need to be converted to yyyy/mm/dd from (YYYY-MM-DDT00:00:00) format.
Contributor
2096 Points
1040 Posts
Re: convert DataTable entire column to YYYY/MM/DD format without for-loop from YYYY-MM-DDT00:00:0...
Jan 21, 2020 07:20 AM|Khuram.Shahzad|LINK
If you use this data table as data source of some data control like Gridview etc then you can apply this formatting at template level/field rendering. ELSE if you need formating of data table column then you can loop through it , get the column by name or id and update the format.
If you have sql query to get data then you can format the date at SQL level , visit this for detail https://stackoverflow.com/questions/17205441/convert-date-format-into-dd-mmm-yyyy-format-in-sql-server