Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to change date time format colum
3/1/2021  5:41:41 PM to Feb-10-2021 10:15 AM


in datatable i have this date formate
3/1/2021  5:41:41 PM
but i want to change to
Feb-10-2021 10:15 AM
in csv file

Period - expected colum 

Feb-10-2021 10:15 AM
Feb-10-2021 10:15 AM
Feb-10-2021 10:15 AM
Feb-10-2021 10:15 AM
Feb-10-2021 10:15 AM
Feb-10-2021 12:00 AM


Period - like this am getting
3/1/2021 17:41
3/1/2021 17:41
3/1/2021 17:41
3/1/2021 17:41
3/1/2021 17:41
3/1/2021 17:41
3/2/2021 22:41
3/2/2021 22:41
3/2/2021 22:41


What I have tried:

private void GetInputDateRange()
        {
            DateTime startDate = System.DateTime.Now.Date;
            DateTime endDate = System.DateTime.Now.Date;
            DataTable dtLicenceAvgData = new DataTable();
            DataTable dtDetailUsageData = new DataTable();
            DataTable dtOverUsageLicenseData = new DataTable();
            int week = 0;
            if (cmbDatePrd.SelectedItem.ToString() != string.Empty && cmbDatePrd.SelectedItem.ToString() != "Please Select")
            {
                objUtilities = new Utilities();
                week = txtWeek.Text != "" ? Convert.ToInt32(txtWeek.Text) : 0;
                startDate = objUtilities.BuildDateRange(cmbDatePrd.SelectedItem.ToString().Trim(), week, out endDate);

            }
            else if (dtTmPckrStartDt.Text != string.Empty && dtTmPckrEndDt.Text != string.Empty)
            {
                startDate = dtTmPckrEndDt.Value.Date;
                endDate = dtTmPckrEndDt.Value.Date;
            }

            dtLicenceAvgData = objDBIO.GetAvgDataBasedOnDateRange(DBPath, startDate, endDate);
            dtDetailUsageData = objDBIO.GetUsageDetailsDataBasedOnDateRange(DBPath, startDate, endDate);
            dtOverUsageLicenseData = objDBIO.GetOverUsageLicenseDataBasedOnDateRange(DBPath, startDate, endDate);
            string filepath = txtExptDir.Text;

            objFileIO.ExtractDataToCSV(dtLicenceAvgData, filepath + "\\Report_AvgLicense_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".csv");
             dtLicenceAvgData.Rows[0]["Period"] = Convert.ToDateTime(dtLicenceAvgData.Rows[0]["Period"]).ToString("mm-dd-yyyy");
            objFileIO.ExtractDataToCSV(dtDetailUsageData,filepath + "\\Report_Complete_Details_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".csv");
            objFileIO.ExtractDataToCSV(dtOverUsageLicenseData,filepath + "\\Report_DeptLicenseOverUsage_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".csv");
        }
Posted
Updated 15-Mar-21 1:38am
v2

1 solution

First off, changing the date format won't change 3/1/2021 to any February date: it's either the 3rd of January, or the 1st March.

And if it's this row you are talking about then it's trivial:
C#
dtLicenceAvgData.Rows[0]["Period"] = Convert.ToDateTime(dtLicenceAvgData.Rows[0]["Period"]).ToString("mm-dd-yyyy");
Just change the format string you specify in the ToString call to "dd-MMM-yyyy hh:mm tt"
Do note the case is important here, and your original code is wrong: "mm" does not select months, it selects minutes. "MM" is months as a number.
This may help: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
Member 15088142 15-Mar-21 7:57am    
i am getting this error System.ArgumentException: 'String was not recognized as a valid DateTime.Couldn't store <mar-01-2021 05:41:pm=""> in Period Column. Expected type is DateTime.'
Member 15088142 15-Mar-21 7:57am    
dtLicenceAvgData.Rows[0]["Period"] = Convert.ToDateTime(dtLicenceAvgData.Rows[0]["Period"]).ToString("MMM-dd-yyyy hh:mm:tt");
Member 15088142 15-Mar-21 8:07am    
dtLicenceAvgData.Rows[0]["Period"] = Convert.ToDateTime(dtLicenceAvgData.Rows[0]["Period"]).ToString("MMM-dd-yyyy hh:mm tt");
now i am not getting error but still its not converted to Mar-10-2021 10:15 AM format
OriginalGriff 15-Mar-21 8:20am    
Do yourself a favour and read the code you just posted: why the heck are you converting something to a string in order to immediately convert it back to what it started out as?

Now that you shared your error message, it's obvious what the p[roblem is: you DataTable is expecting a DateTime, and those do not have any format associated with them. What you need to do is apply a format to the column of the presentation control so that it formats the data the way you want it, not try to faff about with strings...
Member 15088142 15-Mar-21 9:01am    
i didnt get it how to change format

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900