HEEN
HEEN

Reputation: 4721

Date is not taking proper format as Expected (dd/MM/yyyy)

I want to insert date into database in dd/MM/yyyy format. For that I have written like below:

drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
    ? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]);

And it is working perfectly fine on my local machine, but on my server it is taking format as dd/MM/yyyy hh:mm:ss. So how to set the same format there too. Kindly suggest.

Upvotes: 0

Views: 550

Answers (4)

krlzlx
krlzlx

Reputation: 5822

Use DateTime.ToShortDateString:

drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
    ? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]).ToShortDateString();

But, I suggest you keep the time part of your date (without using the ToShortDateString as it will insert a time set to midnight, cf. highlighted text in Oracle documentation below) when inserting and get the format you want (without time) when you're using the date.

From Oracle documentation:

Oracle Database automatically converts character values that are in the default date format into date values when they are used in date expressions.

If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.

Oracle Database DATE columns always contain fields for both date and time. If your queries use a date format without a time portion, then you must ensure that the time fields in the DATE column are set to midnight. You can use the TRUNC (date) SQL function to ensure that the time fields are set to midnight, or you can make the query a test of greater than or less than (<, <=, >=, or >) instead of equality or inequality (= or !=). Otherwise, Oracle Database may not return the query results you expect.

So you can convert you're date in any format you want in c#, removing the time part, Oracle will automatically set the time part of your date as midnight (00:00:00).

Upvotes: 1

Eldaniz Ismayilov
Eldaniz Ismayilov

Reputation: 856

Try:

public static DateTime? DateFromString(string value)
{
   if (string.IsNullOrWhiteSpace(value))
     {
       return null;
     }
   else
     {
        return DateTime.ParseExact(dateString, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
     }
}

DateTime? date=DateFromString(e.Record["CHEQUE_DT"]);
drExpInfo[0]["CHEQUE_DT"] = date==null? DBNull.Value : (object)date);

Upvotes: 0

Kathir07
Kathir07

Reputation: 231

Check this Discussion Here you can find two or more ways to setting date format while insert records into database.

insert into sampleDate(Start_Date) values (to_date('25-Jun-2017','YYYYMMDD'))

Upvotes: 0

Mehmet
Mehmet

Reputation: 1874

You can set CultureInfo;

var cultureInfo = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime(e.Record["CHEQUE_DT"], cultureInfo).ToString();

Upvotes: 0

Related Questions