-1

I am getting GMT DateTime as string input. For example

SampleDate = "20170221T085258.732 GMT"

Now, I want to convert this to datetime object. What is the best way of doing this conversion?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Tanmay
  • 325
  • 1
  • 5
  • 13

4 Answers4

1

You just need to use ToLocalTime() Then you can change it to whatever timezone you care about.

 DateTimeOffset.Parse(SampleDate).ToLocalTime();
chris-crush-code
  • 1,114
  • 2
  • 8
  • 17
1
var offset = new Date().getTimezoneOffset();

To remove the GMT and time zone, change the following line:

document.write(d.toString().replace(/GMT.*/g,""));
Naive
  • 345
  • 2
  • 6
  • 18
1

Hi try this code using DateTime.ParseExact()

 string SampleDate=""20170221T085258.732 GMT";  
 DateTime dateObject = DateTime.ParseExact(SampleDate,"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz",  System.Globalization.CultureInfo.InvariantCulture);

For more info heres the link for DateTime.ParseExact MSDN: https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

Drew Aguirre
  • 375
  • 2
  • 15
0

Below code worked for me. Date contain some unwanted characters like "T",".","GMT" , once I removed those, it started working..

But I feel that, there has to be some better solution for this.

    //I can write a regular expression to keep only numeric values and avoid this replacements...
    SampleDate = "20170221T085258.732 GMT"
    SampleDate = SampleDate.Replace("GMT", "")
    SampleDate = SampleDate.Replace("T", "")
    SampleDate = SampleDate.Replace(".", "")

    Dim dateObject As DateTime = DateTime.ParseExact(SampleDate.Trim(), "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.InvariantCulture)
Tanmay
  • 325
  • 1
  • 5
  • 13
  • You should edit your question to include this information and then delete this answer. If you do this, there is a chance that the question may be re-opened, but I suspect that it will be marked as a duplicate. Also, your conversion will only be accurate if the computer executing the code is configured as being in the GMT timezone. – TnTinMn May 13 '17 at 02:21