Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
"Wed May 03 2017 05:04:00 GMT+0530 (India Standard Time)" from this string only want
"May 03 2017 05:04:00" this part.


how to get only selected part from string?

What I have tried:

int[] arrDate = GetDateTime.Split(',').Select(int.Parse).ToArray();
 
but it shows Error:- Input string was not in a correct format. 
Posted
Updated 22-May-17 2:55am
Comments
CHill60 22-May-17 8:11am    
F-ES Sitecore 22-May-17 8:24am    
You can get the "IndexOf" "GMT" and take everything before it, or "Split" on "GMT" and take the first element in the resulting array.
Shridhar Salunkhe 22-May-17 8:40am    
how to covert in array and get indexof array?
Shridhar Salunkhe 22-May-17 8:24am    
how to convert it into array?

Quote:
"Wed May 03 2017 05:04:00 GMT+0530 (India Standard Time)" from this string only want "May 03 2017 05:04:00" this part.


No, you don't. You need almost entire text to be able to convert that string into date, at least: Wed May 03 2017 05:04:00+0530
I answered your previous question here: Convert string to date format?[^]

C#
string sdate = "Mon May 22 2017 12:48:00 GMT+0530 (India Standard Time)";
int bracketposition = sdate.IndexOf("(");
sdate = sdate.Substring(0,bracketposition-1).Trim();
sdate = sdate.Replace("GMT", "");
DateTimeOffset dto = DateTimeOffset.Parse(sdate);

Console.WriteLine("India standard time '{0}' => Local time: '{1}'", sdate, dto.ToLocalTime());
Console.WriteLine("India standard time '{0}' => Universal time: '{1}'", sdate, dto.ToUniversalTime());

Result:
India standard time 'Mon May 22 2017 12:48:00 +0530' => Local time: '2017-05-22 09:18:00 +02:00'
India standard time 'Mon May 22 2017 12:48:00 +0530' => Universal time: '2017-05-22 07:18:00 +00:00'
 
Share this answer
 
v3
Try a regex:
(?=<\w+\s)\w+\s\d+\s\d+\s\d+:\d+:\d+
Should do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900