Link to home
Start Free TrialLog in
Avatar of GRChandrashekar
GRChandrashekarFlag for India

asked on

Datatable

Following function returns datatable containing two strings firstdayoflastmonth, lastdayoflastmonth

 public static DataTable returnfirstlastdateofpreviousmonth()
        {
            var Viewpreviousmonthfirstlastdate = new VPreviousmonthfirstlastdateTypedView();
            var adapterpreviousmonthfirstlastdate = new DataAccessAdapter();
            adapterpreviousmonthfirstlastdate.FetchTypedView(Viewpreviousmonthfirstlastdate.GetFieldsInfo(), Viewpreviousmonthfirstlastdate);
            var DataTablepreviousmonthfirstlastdate = new DataTable();
            DataTablepreviousmonthfirstlastdate.Clear();
            DataTablepreviousmonthfirstlastdate = Viewpreviousmonthfirstlastdate;
            return DataTablepreviousmonthfirstlastdate;
        }

I need to modify this function to return 2 outputs in date format without time for fields firstdayoflastmonth, lastdayoflastmonth
Avatar of vsosu
vsosu

You cannot return 2 output but in the parameter you can return it.

eg.
public static DataTable returnfirstlastdateofpreviousmonth(int firstdayoflastmonth, int lastdayoflastmonth )
{
....
firstdayoflastmonth = ..
lastdayoflastmonth = ..
....
}
Avatar of GRChandrashekar

ASKER

since it is data table i need to convert to date format and not int.
I have given a sample only and you can return data table in the parameter.
Use Convert.ToDateTime(..) to convert string into date format
This line
 processmonthend.Monthenddate =
                        Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]);

returns date and time. How do I format this to get only date ?
Avatar of Meir Rivkin
to get only date use the following format: MM/dd/yyyy

 processmonthend.Monthenddate =
                        DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));

You can get using tostring as below:
processmonthend.Monthenddate.ToString("dd/MM/yyyy")
if you want the function to return 2 outputs, instead of return datatable you can apply the following:

public static void returnfirstlastdateofpreviousmonth(out DateTime firstdayoflastmonth, out DateTime lastdayoflastmonth)
        {
            firstdayoflastmonth= DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["FIRSTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));

lastdayoflastmonth = DateTime.Parse(Convert.ToDateTime(DBLastDateOfLastMonth.Rows[0]["LASTDAYOFLASTMONTH"]).ToString("MM/dd/yyyy"));
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of starlite551
starlite551
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@starlite551

>>Perhaps this would work better

it ain't better, it simply different way of achieving the same result.
I think ToShortDateString() is better than hard-coded format such as ToString("MM/dd/yyyy")
It's simply because ToShortDateString() uses the current culture of the running application and thus will output the better result.
i.e. on fr-FR culture, it will format as dd/MM/yyyy and on en-US, it will show MM/dd/yyyy
it depends in the requirements, if the value is stored in DB should be in specific format than hard-coded format is the way to go.