0

I have a set of array.

//this is not hard corded, some times array will have multiple no.of strings in date format.
["vishnu","2016-08-31T18:30:00.000Z","1992","banglore"] 

I have an array of strings, among these strings there is one string which is in date format.

I need to do a foreach and need to check which string is in the date format. If we got the date string "2016-08-30T18:30:00.000Z" I need to convert it to basic date format but in correct timezone, here the date is 2016-08-31 but what I need as out put is

["vishnu","31/8/2016","1992","banglore"]

not

//check the difference in date!
["vishnu","30/8/2016","1992","banglore"]

the aim is from the array, if string is in date string format, convert it.

public static void Main(string[] args)
{                        
    string inputString = "2016-08-31T18:30:00.000Z";
    DateTime enteredDate = DateTime.Parse(inputString);
    Console.WriteLine(enteredDate);
    DateTime dDate;

    if (DateTime.TryParse(inputString, out dDate))
    {
        DateTime dtx = enteredDate.ToLocalTime();
        String.Format("{0:d/MM/yyyy}", dDate); 
        Console.WriteLine(dtx);
    }
    else
    {
        Console.WriteLine("Invalid"); // <-- Control flow goes here
    }

   // DateTime dt = convertedDate.ToLocalTime();
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
vishnuprasad kv
  • 990
  • 5
  • 22
  • 46

3 Answers3

1

If you need to correct the DateTime for the time zone, you can use TimezoneInfo.ConvertTime():

string inputString = "2016-08-31T18:30:00.000Z";

DateTime dDate;

if (DateTime.TryParse(inputString, out dDate))
{
    DateTime correctedDateTime = TimeZoneInfo.ConvertTime(dDate, TimeZoneInfo.Local);

    // write this here back into the array using your format
    Console.WriteLine(correctedDateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
}
else
{
    Console.WriteLine("Invalid"); // <-- Control flow goes here
}

For further reference check out this post. This answer is inspired by it to use TimeZoneInfo.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0
     DateTime dDate;

do this operation iside foreach

        if (DateTime.TryParse(answerString, out dDate))
        {
            DateTime enteredDate = DateTime.Parse(answerString);
            var Date = enteredDate.ToString("dd/MM/yyyy");
            answerString = Date;
           Console.WriteLine(answerString);
        }
    else{
    //operation
        }

thanks to mong zhu

vishnuprasad kv
  • 990
  • 5
  • 22
  • 46
-1

Try using DateTimeOffset rather than DateTime as it is built to handle time zones.

Here's the code:

string inputString = "2016-08-31T18:30:00.000Z";

DateTimeOffset enteredDate = DateTimeOffset.Parse(inputString);
Console.WriteLine(enteredDate);

DateTimeOffset dtx = enteredDate.ToLocalTime();
Console.WriteLine(dtx);

This produces the following for me in GMT+09:30:

2016/08/31 18:30:00 +00:00
2016/09/01 04:00:00 +09:30

To get it in Indian time try this:

DateTimeOffset dtx = enteredDate.ToOffset(TimeSpan.FromHours(5.5));
Console.WriteLine(dtx);

I get 2016/09/01 00:00:00 +05:30 now.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Although the answer youre giving is would work, should the users application get shipped to another country \ time zone then the hours you're entering would be totally out. For instance BST, GMT and UTC are all times referenced within the UK, or if the user is in a CET time zone and then moves to a PT time zone all figures would be out. – Simon Price Sep 29 '16 at 08:44
  • @SimonPrice - Yes, that's why I showed the `.ToLocalTime()` call. It would work perfectly fine in any user's time zone. So, the OP could simply use that without the `.ToOffset(TimeSpan.FromHours(5.5))`. – Enigmativity Sep 29 '16 at 09:37
  • My Apologies, I missed that line – Simon Price Sep 29 '16 at 10:02