How to convert timezone offset text "+01:00" to timespan - c#

I have an API which will accept timezone offset as string. I need to convert the timezone to TimeSpan and add the timespan with the data i have which is in UTC. Here is what i'm trying.
private bool TryGetHrAndMinFromTimeZone(string timeZone, out TimeSpan result)
{
try
{
var isPositive = !timeZone.StartsWith("-");
var hrAndMin = string.Concat(timeZone.Where(x => x != '-' && x != '+')).Split(':');
var hr = int.Parse(hrAndMin[0]);
var min = int.Parse(hrAndMin[1]);
result = isPositive ? new TimeSpan(hr, min, 0) : new TimeSpan(-hr, -min, 0);
return true;
}
catch (Exception)
{
throw new Exception(string.Format("Provided TimeZone '{0}' is Invalid ", timeZone));
}
}
Is there any better option to do it?

you can try
TimeSpan.TryParse("-07:00", out TimeSpan ts) //for -07:00
TimeSpan.TryParse("07:00", out TimeSpan ts) //for +07:00
for more information https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones#converting-datetimeoffset-values

The DateTimeOffset type can parse offsets of this format using the zzz specifier. Thus you can write a function such as the following:
static TimeSpan ParseOffset(string s)
{
return DateTimeOffset.ParseExact(s, "zzz", CultureInfo.InvariantCulture).Offset;
}
Another approach, you can parse with TimeSpan.ParseExact if you strip off the sign first and handle it yourself:
static TimeSpan ParseOffset(string s)
{
var ts = TimeSpan.ParseExact(s.Substring(1), #"hh\:mm", CultureInfo.InvariantCulture);
return s[0] == '-' ? ts.Negate() : ts;
}
Or, as Manish showed in his answer, you can let TimeSpan.Parse attempt to figure out the string. It works if you remove the + sign first.
static TimeSpan ParseOffset(string s)
{
return TimeSpan.Parse(s.Replace("+", ""), CultureInfo.InvariantCulture);
}

Related

How to convert a string to DateTime in a TextBox of a gridview [duplicate]

How do you convert a string such as 2009-05-08 14:40:52,531 into a DateTime?
Since you are handling 24-hour based time and you have a comma separating the seconds fraction, I recommend that you specify a custom format:
DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",
System.Globalization.CultureInfo.InvariantCulture);
You have basically two options for this. DateTime.Parse() and DateTime.ParseExact().
The first is very forgiving in terms of syntax and will parse dates in many different formats. It is good for user input which may come in different formats.
ParseExact will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. This way, you can easily detect any deviations from the expected data.
You can parse user input like this:
DateTime enteredDate = DateTime.Parse(enteredString);
If you have a specific format for the string, you should use the other method:
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);
"d" stands for the short date pattern (see MSDN for more info) and null specifies that the current culture should be used for parsing the string.
try this
DateTime myDate = DateTime.Parse(dateString);
a better way would be this:
DateTime myDate;
if (!DateTime.TryParse(dateString, out myDate))
{
// handle parse failure
}
Use DateTime.Parse(string):
DateTime dateTime = DateTime.Parse(dateTimeStr);
Nobody seems to implemented an extension method. With the help of #CMS's answer:
Working and improved full source example is here: Gist Link
namespace ExtensionMethods {
using System;
using System.Globalization;
public static class DateTimeExtensions {
public static DateTime ToDateTime(this string s,
string format = "ddMMyyyy", string cultureString = "tr-TR") {
try {
var r = DateTime.ParseExact(
s: s,
format: format,
provider: CultureInfo.GetCultureInfo(cultureString));
return r;
} catch (FormatException) {
throw;
} catch (CultureNotFoundException) {
throw; // Given Culture is not supported culture
}
}
public static DateTime ToDateTime(this string s,
string format, CultureInfo culture) {
try {
var r = DateTime.ParseExact(s: s, format: format,
provider: culture);
return r;
} catch (FormatException) {
throw;
} catch (CultureNotFoundException) {
throw; // Given Culture is not supported culture
}
}
}
}
namespace SO {
using ExtensionMethods;
using System;
using System.Globalization;
class Program {
static void Main(string[] args) {
var mydate = "29021996";
var date = mydate.ToDateTime(format: "ddMMyyyy"); // {29.02.1996 00:00:00}
mydate = "2016 3";
date = mydate.ToDateTime("yyyy M"); // {01.03.2016 00:00:00}
mydate = "2016 12";
date = mydate.ToDateTime("yyyy d"); // {12.01.2016 00:00:00}
mydate = "2016/31/05 13:33";
date = mydate.ToDateTime("yyyy/d/M HH:mm"); // {31.05.2016 13:33:00}
mydate = "2016/31 Ocak";
date = mydate.ToDateTime("yyyy/d MMMM"); // {31.01.2016 00:00:00}
mydate = "2016/31 January";
date = mydate.ToDateTime("yyyy/d MMMM", cultureString: "en-US");
// {31.01.2016 00:00:00}
mydate = "11/شعبان/1437";
date = mydate.ToDateTime(
culture: CultureInfo.GetCultureInfo("ar-SA"),
format: "dd/MMMM/yyyy");
// Weird :) I supposed dd/yyyy/MMMM but that did not work !?$^&*
System.Diagnostics.Debug.Assert(
date.Equals(new DateTime(year: 2016, month: 5, day: 18)));
}
}
}
I tried various ways. What worked for me was this:
Convert.ToDateTime(data, CultureInfo.InvariantCulture);
data for me was times like this 9/24/2017 9:31:34 AM
Try the below, where strDate is your date in 'MM/dd/yyyy' format
var date = DateTime.Parse(strDate,new CultureInfo("en-US", true))
Convert.ToDateTime or DateTime.Parse
DateTime.Parse
Syntax:
DateTime.Parse(String value)
DateTime.Parse(String value, IFormatProvider provider)
DateTime.Parse(String value, IFormatProvider provider, DateTypeStyles styles)
Example:
string value = "1 January 2019";
CultureInfo provider = new CultureInfo("en-GB");
DateTime.Parse(value, provider, DateTimeStyles.NoCurrentDateDefault););
Value: string representation of date and time.
Provider: object which provides culture specific info.
Styles: formatting options that customize string parsing for some date and time parsing methods. For instance, AllowWhiteSpaces is a value which helps to ignore all spaces present in string while it parse.
It's also worth remembering DateTime is an object that is stored as number internally in the framework, Format only applies to it when you convert it back to string.
Parsing converting a string to the internal number type.
Formatting converting the internal numeric value to a readable
string.
I recently had an issue where I was trying to convert a DateTime to pass to Linq what I hadn't realised at the time was format is irrelevant when passing DateTime to a Linq Query.
DateTime SearchDate = DateTime.Parse(searchDate);
applicationsUsages = applicationsUsages.Where(x => DbFunctions.TruncateTime(x.dateApplicationSelected) == SearchDate.Date);
Full DateTime Documentation
string input;
DateTime db;
Console.WriteLine("Enter Date in this Format(YYYY-MM-DD): ");
input = Console.ReadLine();
db = Convert.ToDateTime(input);
//////// this methods convert string value to datetime
///////// in order to print date
Console.WriteLine("{0}-{1}-{2}",db.Year,db.Month,db.Day);
You could also use DateTime.TryParseExact() as below if you are unsure of the input value.
DateTime outputDateTimeValue;
if (DateTime.TryParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out outputDateTimeValue))
{
return outputDateTimeValue;
}
else
{
// Handle the fact that parse did not succeed
}
I just found an elegant way:
Convert.ChangeType("2020-12-31", typeof(DateTime));
Convert.ChangeType("2020/12/31", typeof(DateTime));
Convert.ChangeType("2020-01-01 16:00:30", typeof(DateTime));
Convert.ChangeType("2020/12/31 16:00:30", typeof(DateTime), System.Globalization.CultureInfo.GetCultureInfo("en-GB"));
Convert.ChangeType("11/شعبان/1437", typeof(DateTime), System.Globalization.CultureInfo.GetCultureInfo("ar-SA"));
Convert.ChangeType("2020-02-11T16:54:51.466+03:00", typeof(DateTime)); // format: "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz"
Put this code in a static class> public static class ClassName{ }
public static DateTime ToDateTime(this string datetime, char dateSpliter = '-', char timeSpliter = ':', char millisecondSpliter = ',')
{
try
{
datetime = datetime.Trim();
datetime = datetime.Replace(" ", " ");
string[] body = datetime.Split(' ');
string[] date = body[0].Split(dateSpliter);
int year = date[0].ToInt();
int month = date[1].ToInt();
int day = date[2].ToInt();
int hour = 0, minute = 0, second = 0, millisecond = 0;
if (body.Length == 2)
{
string[] tpart = body[1].Split(millisecondSpliter);
string[] time = tpart[0].Split(timeSpliter);
hour = time[0].ToInt();
minute = time[1].ToInt();
if (time.Length == 3) second = time[2].ToInt();
if (tpart.Length == 2) millisecond = tpart[1].ToInt();
}
return new DateTime(year, month, day, hour, minute, second, millisecond);
}
catch
{
return new DateTime();
}
}
In this way, you can use
string datetime = "2009-05-08 14:40:52,531";
DateTime dt0 = datetime.TToDateTime();
DateTime dt1 = "2009-05-08 14:40:52,531".ToDateTime();
DateTime dt5 = "2009-05-08".ToDateTime();
DateTime dt2 = "2009/05/08 14:40:52".ToDateTime('/');
DateTime dt3 = "2009/05/08 14.40".ToDateTime('/', '.');
DateTime dt4 = "2009-05-08 14:40-531".ToDateTime('-', ':', '-');
String now = DateTime.Now.ToString("YYYY-MM-DD HH:MI:SS");//make it datetime
DateTime.Parse(now);
this one gives you
2019-08-17 11:14:49.000
Different cultures in the world write date strings in different ways. For example, in the US 01/20/2008 is January 20th, 2008. In France this will throw an InvalidFormatException. This is because France reads date-times as Day/Month/Year, and in the US it is Month/Day/Year.
Consequently, a string like 20/01/2008 will parse to January 20th, 2008 in France, and then throw an InvalidFormatException in the US.
To determine your current culture settings, you can use System.Globalization.CultureInfo.CurrentCulture.
string dateTime = "01/08/2008 14:50:50.42";
DateTime dt = Convert.ToDateTime(dateTime);
Console.WriteLine("Year: {0}, Month: {1}, Day: {2}, Hour: {3}, Minute: {4}, Second: {5}, Millisecond: {6}",
dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
This worked for me:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dt = DateTime.ParseExact("2009-05-08 14:40:52,531","yyyy-MM-dd HH:mm:ss,fff", provider);
Do you want it fast?
Let's say you have a date with format yyMMdd.
The fastest way to convert it that I found is:
var d = new DateTime(
(s[0] - '0') * 10 + s[1] - '0' + 2000,
(s[2] - '0') * 10 + s[3] - '0',
(s[4] - '0') * 10 + s[5] - '0')
Just, choose the indexes according to your date format of choice. If you need speed probably you don't mind the 'non-generic' way of the function.
This method takes about 10% of the time required by:
var d = DateTime.ParseExact(s, "yyMMdd", System.Globalization.CultureInfo.InvariantCulture);

Checking if a string contains a regex in the form of a date in c#

I have a string variable defined as :
string str = Request[columns[2][search]];
Sometimes the str returns me a value of AR and sometimes 15/02/2018 to 23/04/2018
Therefore I am checking if the str contains 15/02/2018 to 23/04/2018, then it should return me true.
To perform this check I have used the below code, which does not seem to work. It always returns me false. Can someone please help me with this or by using a regex as an alternative ?
DateTime date;
Boolean isValidDate = DateTime.TryParseExact(str, "dd/MM/yyyy to dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
I would do this in two passes:
Use a regular expression to extract the dates if they're present at all.
Use DateTime.TryParseExact to check that each date really is a date.
I would recommend against trying to do full date validation in the regular expression itself - that's more in the DateTime domain. The regular expression domain is more "finding the bit of text that might be a date".
Here's a complete example of this approach:
using System;
using System.Globalization;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
Test("AR");
Test("99/99/9999 to 99/99/9999");
Test("15/02/2018 to 23/04/2018");
}
static void Test(string text)
{
var result = TryParseDates(text, out var from, out var to);
Console.WriteLine(result
? $"{text}: match! From={from:yyyy-MM-dd}; To={to:yyyy-MM-dd}"
: $"{text}: no match");
}
static readonly Regex dateToDatePattern = new Regex(#"^(\d{2}/\d{2}/\d{4}) to (\d{2}/\d{2}/\d{4})$");
static bool TryParseDates(string text, out DateTime from, out DateTime to)
{
var match = dateToDatePattern.Match(text);
if (match.Success)
{
// Don't assign values to the out parameters until we know they're
// both valid.
if (DateTime.TryParseExact(match.Groups[1].Value, "dd/MM/yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out var tempFrom)
&&
DateTime.TryParseExact(match.Groups[2].Value, "dd/MM/yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out var tempTo))
{
from = tempFrom;
to = tempTo;
return true;
}
}
// Either the regex or the parsing failed. Either way, set
// the out parameters and return false.
from = default(DateTime);
to = default(DateTime);
return false;
}
}
Output:
AR: no match
99/99/9999 to 99/99/9999: no match
15/02/2018 to 23/04/2018: match! From=2018-02-15; To=2018-04-23
You need to create a method to parse and validate the dates:
public bool TryParseDates(string dateString, out DateTime date1, out DateTime date2)
{
var parts = dateString.Split(new [] { " to " }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
{
date1 = default(DateTime);
date2 = default(DateTime);
return false;
}
var date1Result = DateTime.TryParseExact(parts[0], "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date1);
var date2Result = DateTime.TryParseExact(parts[1], "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date2);
return date1Result && date2Result;
}
This splits the strings by " to ", checks that you have two items, and then parses eat date individually, and returns true if it was successful.
Usage:
DateTime d1, d2;
bool isValid = TryParseDates(str, out d1, out d2);
Sample: http://rextester.com/WCEYI66887

Correct format to input date with time of the day

I want to be able to calculate the time in hours and minutes elapsed between, say, 12:35pm 02/13/2016 to 1:45pm 02/14/2016, but can't figure out the correct format to input it.
EDIT: Should add that the span between the times will be stored in an arraylist, one span per customer.
Basically, you need something like this:
var dateA = new DateTime(2016,2,13,12,35,0);
var dateB = new DateTime(2016,2,14,1,45,0);
var timespan = dateB - dateA;
var hours = timespan.Hours;
bar minutes = timespan.Minutes;
Here's how I would go about it:
Func<string, DateTime?> tryParse = t =>
{
DateTime output;
if (DateTime.TryParseExact(
t,
new [] { "h:mmtt MM/dd/yyyy" },
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.AssumeLocal,
out output))
{
return output;
}
return null;
};
var dt1 = tryParse("12:35pm 02/13/2016");
var dt2 = tryParse("1:45pm 02/14/2016");
TimeSpan? ts = null;
if (dt1.HasValue && dt2.HasValue)
{
ts = dt2.Value.Subtract(dt1.Value);
}
if (ts.HasValue)
{
Console.WriteLine(
String.Format(
"{0} hours & {1} minutes",
ts.Value.Hours,
ts.Value.Minutes));
}

Parsing Integer Value As Datetime

I have date represented as integer like 20140820 and I want to parsing it as datetime, like 2014.08.20.
Do I need to parse each integer value (2014)(08)(02) using index or is there simpler way?
If your CurrentCulture supports yyyyMMdd format as a standard date and time format, you can just use DateTime.Parse method like;
int i = 20140820;
DateTime dt = DateTime.Parse(i.ToString());
If it doesn't support, you need to use DateTime.ParseExact or DateTime.TryParseExact methods to parse it as custom date and time format.
int i = 20140820;
DateTime dt;
if(DateTime.TryParseExact(i.ToString(), "yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Then you can format your DateTime with .ToString() method like;
string formattedDateTime = dt.ToString("yyyy.MM.dd", CultureInfo.InvariantCulture);
The easiest and most performance way would be something like:
int date = 20140820;
int d = date % 100;
int m = (date / 100) % 100;
int y = date / 10000;
var result = new DateTime(y, m, d);
Try This :-
string time = "20140820";
DateTime theTime= DateTime.ParseExact(time,
"yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
OR
string str = "20140820";
string[] format = {"yyyyMMdd"};
DateTime date;
DateTime.TryParseExact(str,
format,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out date))
now date variable will have required converted date of string '20140820'
int sampleDate = 20140820;
var dateFormat = DateTime.ParseExact(sampleDate.ToString(), "yyyyMMdd",
CultureInfo.InvariantCulture).ToString("yyyy.MM.dd");
result:
2014.08.20
So, we have two competing implementations,
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
int i = 20140820;
Console.WriteLine($"StringParse:{StringParse.Parse(i)}");
Console.WriteLine($"MathParse:{MathParse.Parse(i)}");
}
}
public static class StringParse
{
public static DateTime Parse(int i)
{
return DateTime.ParseExact(
i.ToString().AsSpan(),
"yyyyMMdd".AsSpan(),
CultureInfo.InvariantCulture);
}
}
public static class MathParse
{
public static DateTime Parse(int i)
{
return new DateTime(
Math.DivRem(Math.DivRem(i, 100, out var day), 100, out var month),
month,
day);
}
}
The string approach is probably safer and probably handles edge cases better.
Both will be fast and unlikely to be a performance bottle neck but it is my untested assertion that the math approach probably has better performance.

How to add timespan values in list

In the below code i will get a list of time span values. I need to add all the time span values and that value has to be stored in string.How to achieve this i tried a lot but I can't find answer for this.
Thanks in Advance.
List<TimeSpan> objList = new List<TimeSpan>();
string totalIntervalTime = string.Empty;
private void Resume_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox2.Text))
{
textBox3.Text = DateTime.Now.ToLongTimeString();
//objPausetmr.Tick += new EventHandler(objPausetmr_Tick);
//objPausetmr.Stop();
tmrObj.Start();
DateTime pausetime = Convert.ToDateTime(textBox2.Text);
DateTime startTime = Convert.ToDateTime(textBox3.Text);
TimeSpan difference = pausetime - startTime;
string intervalDifference = difference.ToString();
richTextBox1.Text = intervalDifference;
TimeSpan tltTime = TimeSpan.Zero;
objList.Add(difference);
foreach (TimeSpan tmVal in objList)
{
tltTime.Add(tmVal);
}
totalIntervalTime = tltTime.ToString();
//MessageBox.Show(interval_Time.ToString());
}
else
{
MessageBox.Show("Please set the Pause time");
}
}
Assuming you want to add values of all time spans into single time span.
DateTime and TimeSpan are immutable structs. All operations using them return new instances. So you need to store result of operation in a TimeSpan value (normally just updating exisintg one is fine)
var totalTime = TimeSpan.Zero;
foreach (TimeSpan currentValue in objList)
{
totalTime = totalTime + currentValue;
}
Usage of + covered in details in TimeSpan.Addition Operator MSDN article.
Alternatively you can use Enumerable.Aggregate:
var totalTime = objList.Aggregate(
(accumulatedValue,current) => accumulatedValue + current);
You can try something like
string s = String.Join(",",objList.Select(x => x.ToString()));
Have a look at
String.Join Method
Enumerable.Select
Using objList.Select(x => x.ToString()) you can determine the format output you want
Span.ToString Method (String)

Categories