-1

I want to convert a datetime to string.

But result now is returned as 04 August, 0016 which is not what I need.

I want result to be 04 August, 2016.

C# code:

DataTable dtGroupCurr = new DataTable();
dtGroupCurr = sourceGroupCurr.Tables[0];

var groupedCurr = (from dt2 in dtGroupCurr.AsEnumerable()
    select new
    {
         S_DATE = dt2.Field<DateTime>("S_DATE"),
         BANK_CODE = dt2.Field<string>("BANK_CODE"),
         BANK_NAME = dt2.Field<string>("BANK_NAME")
    }).Distinct().OrderBy(x => x.S_DATE);

foreach (var s in groupedCurr)
{
     string rDate = s.S_DATE.ToString("yyyy/MM/dd");
     IFormatProvider culture = new CultureInfo("en-US", true);
     DateTime date = DateTime.Parse(rDate, culture);
     string sDate = date.ToString("dd MMMM, yyyy", CultureInfo.InvariantCulture);
}

Thanks in advance ;)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nettoon493
  • 17,733
  • 7
  • 30
  • 45
  • Looks good, take a look : https://dotnetfiddle.net/jPFBrk – sujith karivelil Aug 04 '16 at 03:15
  • I agree with @un-lucky, it looks fine. Expanded version: https://dotnetfiddle.net/jPFBrk .What does the end result in `rDate` look like and is `s.S_Date` a "pure" date time or does it do something else? – Jon P Aug 04 '16 at 03:30
  • 2
    What value had s.S_DATE ? before .ToString("yyyy/MM/dd") – Mate Aug 04 '16 at 03:31
  • @Mate I answer on topic. look code. – nettoon493 Aug 04 '16 at 03:42
  • 2
    great. I can't understand the "extra" parse step. Did you try s.S_DATE.ToString("dd MMMM, yyyy", new CultureInfo("en-US", true)); . if that doesn't work. Please, add s.S_DATE.ToString() output. – Mate Aug 04 '16 at 04:24
  • or maybe try changing s.S_DATE.ToString("yyyy/MM/dd") >> s.S_DATE.ToString("yyyy/MM/dd" , CultureInfo.InvariantCulture) '/' - the date separator. It will be replaced according current culture – Mate Aug 04 '16 at 04:39
  • And we sill don't have clear answer as to the value of `S_Date` because as we have demonstraded the basic code works, so the issue could be in the data. – Jon P Aug 04 '16 at 04:43

3 Answers3

1

Try:

 string sDate = s.S_DATE.ToString("dd MMMM, yyyy", new CultureInfo("en-US", true));

Or

string rDate = s.S_DATE.ToString("yyyy/MM/dd",  CultureInfo.InvariantCulture); // To avoid override 
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime date = DateTime.Parse(rDate, culture);
string sDate = date.ToString("dd MMMM, yyyy", CultureInfo.InvariantCulture);

The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.

MSDN

Mate
  • 4,976
  • 2
  • 32
  • 38
0

Use the DateTime.ParseExact method and specify the format like below:

string rDate = s.S_DATE.ToString("yyyy/MM/dd");
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime date = DateTime.ParseExact(rDate, "yyyy/MM/dd", culture);
string sDate = date.ToString("dd MMMM, yyyy", CultureInfo.InvariantCulture);
Steve Davis
  • 297
  • 1
  • 2
  • 8
0

Use Date or DateTime.ToShortDateString(); Or Date.ToString("dd-MM-yyyy");