I have following string ,
Thu Sep 24 2015 00:00:00 GMT+0530 (IST)
I tried with following but it's faling.
var twDate = DateTime.Parse("Thu Sep 24 2015 00:00:00 GMT+0530 (IST) ");
Can not use replace , as IST wont be fixed. Any Ideas?
You need to trim the time zone abbreviation off using normal string operations, then specify a custom date and time format string. For example:
// After trimming
string text = "Thu Sep 24 2015 00:00:00 GMT+0530";
var dto = DateTimeOffset.ParseExact(
text,
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
CultureInfo.InvariantCulture);
Console.WriteLine(dto);
Note the use of CultureInfo.InvariantCulture here - you almost certainly don't want to parse using the current thread's current culture.
If your string always has the same format and length, you could use this to get UTC:
String dtstr = "Thu Sep 24 2015 00:00:00 GMT+0530 (IST)";
int zoneMin = int.Parse(dtstr.Substring(29, 2)) * 60 + int.Parse(dtstr.Substring(31, 2));
if (dtstr.Substring(28, 1) == "+") zoneMin = -zoneMin;
DateTime dt = DateTime.Parse(dtstr.Substring(0, 24)).AddMinutes(zoneMin);
Related
I have string in "Mon, 20 Mar 2021 14:04:48 +0000"
and I want to convert it as "20 Mar 2021 | 14:04 PM"
I want to convert the string as it is but it was appearing differently in my local and server.
First of all 20 Mar 2021 is Saturday, not Monday, let's correct it. Then you can ParseExact to get DateTime and finally represent it in the required format with a help of ToString():
string source = "Sat, 20 Mar 2021 14:04:48 +0000";
string result = DateTime
.ParseExact(source, "ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)
.ToUniversalTime()
.ToString("dd MMM yyyy' | 'HH:mm tt", CultureInfo.InvariantCulture);
Notes:
It seems that you want to obtain Universal (not Local) time, that's why I've added ToUniversalTime()
14:04 PM looks strange for me (14:04 and 02:04 PM are much more frequent formats); put hh instead of HH to have 02:04 PM
If you actually want to be manipulating the timezone information, then use Noda Time.
If that's your exact text format, and the dates and times are what you want, you can convert it manually:
var input = "Mon, 20 Mar 2021 14:04:48 +0000";
var dateParts = input.Split(' ');
var timeParts = dateParts[4].Split(':');
var amPm = int.Parse(timeParts[0]) < 12 ? "AM" : "PM";
var output = $"{dateParts[1]} {dateParts[2]} {dateParts[3]} | {timeParts[0]}:{timeParts[1]} {amPm}";
System.Console.WriteLine(output); // "20 Mar 2021 | 14:04 PM"
Or, if you are feel adventurous, use a regular expression.
I have a date string in the following format:
var dateString = Fri Jun 26 2020 00:00:00 GMT+0100 (British Summer Time)
How can I convert this to a DateTime in C# such as 26/06/2020 00:00:00
I have tried:
DateTime.Parse(dateString);
And:
DateTime.ParseExact(dateString);
And I get:
System.FormatException: 'String was not recognized as a valid DateTime.'
You can accomplish this by using DateTime.ParseExact and providing a custom date time format. However, this will only work if you first modify the input string to be able to fit the custom date and time format strings that are included in .net.
CultureInfo provider = CultureInfo.InvariantCulture;
var input = "Fri Jun 26 2020 00:00:00 GMT+0100 (British Summer Time)";
// set up a regex that will match the text starting with GMT, and extract just the timezone offset
// (the description of the timezone is irrelevant here)
var r = new Regex(#"GMT([+-]\d\d\d\d) \([\w\s]*\)");
// this will remove the extra text: "Fri Jun 26 2020 00:00:00 +0100"
// now we can match it in our format string
var s = r.Replace(input, "$1");
var f = "ddd MMM dd yyyy hh:mm:ss zzz"; // matches the s variable
var d = DateTime.ParseExact(s, f, provider); // you now have parsed your date
This will include the timezone offset in the DateTime object. If you just want it to be set to "26/06/2020 00:00:00" and to ignore the datetime offset, then just change the regex replace above to replace with String.Empty instead of $1.
This will solve your problem.
var dateString = "Fri Jun 26 2020 00:00:00 GMT + 0100(British Summer Time)"; Console.WriteLine(DateTime.Parse(dateString.Substring(4, 11)));
Hello so what you can do is you can take advantage of "datetime" class and just write this:
DateTime.Now.ToString("MM/dd/yyyy HH:mm");
edit: sorry i forgot to supply the link haha
https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
I have js calender when I select date from calender it calls C# service which takes string as parameter . It is getting date in format Mon Apr 18 2016 00:00:00 GMT 0500 . I want Monday intead of Mon.
My java Script code is
$scope.deliveryDateTime = { Day: new Date(2015, 11, 28, 14, 57) };
var DeliveryDay = $scope.deliveryDateTimeData.Day;
$http.get(meta.service.GetTimeAvlb+'?day='+ DeliveryDay).
success(function (data) {
debugger;
console.log("Data" + data);
$scope.AvailiableTimeData = data.data;
}
}).
error(function (data) {
});
Here is my C# Service code
public ApiResult GetTimeAvlb( string day )
{
var splitDay = day.Split(' ');
string dayName1 = splitDay[0].ToString();
apiresilt.data = db.TimeAvaliblities.Where(ss => ss.DayName == dayName1).ToList();
return apiresilt;
}
here dayName1 returning Mon while my db has value Monday .
If you really wanna parse this string to DateTime, you have to use your GMT 0500 (Pakistan Standard Time) part as a string literal. DateTime parsing methods does not support timezone names and UTC Offset part without TimeSeparator.
Then you can use DateTime.ToString method with dddd specifier and english-based culture like InvariantCulture.
var s = "Mon Apr 18 2016 00:00:00 GMT 0500 (Pakistan Standard Time)";
var dt = DateTime.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT 0500 (Pakistan Standard Time)'",
CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dddd", CultureInfo.InvariantCulture));
By the way, there is no such a thing as date time string. Your data either can be DateTime or string.
.ToString("Format-String") can be used for display date in required format;
DateTime dt = DateTime.Now; will be the current date APR/01/2016
string str = dt.ToString("dddd"); // this will be Friday for 04/01/2016
I have this string "Sun Aug 02 2015 00:15:47 GMT+0000 (UTC)"
I created this much of the datetime format "ddd MMM dd yyyy HH:mm:ss"
Now im not sure what to do with the ending part of that datetime string.
Im not sure if the string I have is a standard format for UTC that can be easily converted or if its a custom format.
Nonetheless, I want to turn that string datetime into a datetime object.
string str = "Sun Aug 02 2015 00:15:47 GMT+0000 (UTC)";
var dt = DateTime.ParseExact(str, "ddd MMM dd yyyy HH:mm:ss \"GMT\"zzzz \"(UTC)\"", CultureInfo.InvariantCulture);
I would consider one of these two approaches:
string str = "Sun Aug 02 2015 00:15:47 GMT+00:00 (UTC)";
str = str.Substring(0, str.IndexOf('(') - 1);
DateTime dt = DateTime.ParseExact(str, "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
or
string str = "Sun Aug 02 2015 00:15:47 GMT+00:00 (UTC)";
str = str.Substring(0, str.IndexOf('(') - 1);
DateTimeOffset dto = DateTimeOffset.ParseExact(str, "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
CultureInfo.InvariantCulture);
In either example, we assume that the part in parenthesis is irrelevant. This may be important if your input can vary with different time zones.
In the first example, we assume that you want the output to always be a UTC based DateTime. The input offset could vary, but the output will always be adjusted to Coordinated Universal Time, and will have DateTimKind.Utc.
In the second example, we assume that you want the output to match exactly what was provided in the input. To do this, the output needs to be a DateTimeOffset type. Otherwise, you wouldn't be able to track offsets that didn't exactly match UTC or your own local time zone.
I prefer the second option. If you need a DateTime, you can always get one by calling the .DateTime, .UtcDateTime, or .LocalDateTime properties of the resulting DateTimeOffset.
Am unable to convert a string which represents date and time ex: "Tue Mar 18 14:37:34 PDT 2014" to a DateTime object. From the format I can figure it out to be in the RFC 1123 format. What is the best way to parse date strings as above?
Timezone literals are not supported by DateTime.Parse/ParseExact. Here is a workaround:
string inputDate = "Tue Mar 18 14:37:34 PDT 2014";
inputDate = inputDate.Replace("PDT", "-7");
DateTime d = DateTime.ParseExact(inputDate, "ddd MMM dd HH:mm:ss z yyyy", culture);
Console.WriteLine(d);
If you can make the format of the string like this (you are pretty close):
Sat, 01 Nov 2008 19:35:00 GMT
You can use DateTime.Parse(dateString);
Find more information here http://msdn.microsoft.com/en-us/library/vstudio/1k1skd40(v=vs.100).aspx