简体   繁体   中英

How can I parse Kusto timespan strings in C#?

I tried TimeSpan.Parse("2d") , for example, but that doesn't work.

The format of Kusto timespan doesn't seem to be supported by any of the TimeSpan.Parse() flavors.

The Kusto Data Client SDK for .NET (in the Microsoft.Azure.Kusto.Data nuget package) has a CslTimeSpanLiteral class that understands this format. It contains several static methods for parsing strings to .NET's TimeSpan structure.

For example:

using Kusto.Data.Common;

...

TimeSpan? ts = CslTimeSpanLiteral.Parse("2d");

In addition to Parse , there is also ParseNoNull , TryParse , and TryParseNoNull .

TimeSpan.Parse in c# does not recognize the Kusto timespan strings like 2d,2h etc.

We don't know your purpose on parsing it, but you can use some code like below:

        string mytime = "2d";

        if (mytime.EndsWith("d"))
        {
            mytime = mytime.Remove(mytime.IndexOf('d'));
            var dt = DateTime.Now.AddDays(Convert.ToDouble(mytime));
            Console.WriteLine(dt.ToString());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM