How to use Windows.Globalization.DateTimeFormatting to get a "xx minutes" string?


All replies

  • Yes, you can format any DateTime with the DateTimeFormatter class. It can be a duration rather than an actual time. See the remarks in the DateTimeFormatter docs for the list of valid templates.
    Sunday, April 6, 2014 12:18 AM
  • Sounds more like you need a TimeSpan than a DateTime.

    For example:

    DateTime start = new DateTime(2010, 01,01 12,55,00);
    DateTime end = DateTime.Now;
    Timespan ts = end-start;
    Debug.WriteLine(string.Format("{0} days, {1} hours, {2} minutes", ts.Days, ts.Hours, ts.Minutes));

    Darin R.

    Sunday, April 6, 2014 7:11 PM
  • U sure there is a Timespan in JavaScript? :)

    Also I would like to use the Windows Globalization API to correctly format minutes and hours, not format it myself.

    @Rob: I did not find something for timespan/duration formatting in the remarks.

    Sunday, April 6, 2014 8:08 PM
  • Sorry, I didn't see the forum (or didn't comprehend it...) bad me.



    Darin R.

    Sunday, April 6, 2014 9:51 PM
  • Don't back off so quickly, Darin! The Windows.Foundation.TimeSpan structure is available to JavaScript.

    PKursawe, take a look at the Date and time formatting sample . Scenario 2 demonstrates using the hour minute string format. If you want something more specific that isn't templated then you'll need to create that yourself.


    Sunday, April 6, 2014 11:11 PM
  • To clarify, 'minutes' in the question are within the quotes. - I'm not aware that DateTimeFormatter has the localized names for "days", "hours", "minutes" or their abbreviations "d", "h", "m".  This was an approach I took, using resources.

    return string.Format("{0:N0} {1}", timespan.TotalMinutes, ResoureManager.ResourceLoader.GetString("TimeUnitMinutesAbbreviationLower/Text"));

    C# not JavaScript but am sure you get the gist.

    (However, if localized period names singular/plural and abbreviations can also be output it would be helpful to know, and what these formatting strings are - didn't seem clear in the sample)


    • Edited by Ben - GGT Monday, April 7, 2014 2:08 PM clarity
    Monday, April 7, 2014 2:08 PM