Is there an easy way to convert seconds to ISO 8601 format for use in schema.org video metadata?

I am trying to use what google suggests for html markup in video search results. They suggest using schema.org markup for SEO purposes.

The next line relates to duration: <meta itemprop="duration" content="T1M33S" />

Time must be in ISO 8601 format according to Google and Schema.org - "Video length in ISO 8601 format".

I have some jquery that pulls the duration of a video from an html5 video element. The allocated duration is in seconds.

What is the best way to convert seconds to ISO 8601 format?

Is there something in C #, Javascript or JQuery to accomplish this task easily?

thank

+3


source to share


2 answers


In C #, you can use TimeSpan and custom format string:

var duration = TimeSpan.FromSeconds(secs);

string iso8601 = duration.ToString("'P'd'DT'h'H'm'M's'S'");

      



(nb I didn't actually run this)

0


source


JQuery is not needed to get this result. However, JavaScript can be used for this.

First, you can use the function toISOString()

in the date class:

var date = new Date();
date.toISOString();

      



However, this is not supported in all browsers because it is part of ECMAScript5 (most browsers, but with caution). So you can write a backup to do this, for example:

if ( !Date.prototype.toISOString ) {
  ( function() {

    function pad(number) { // Add leading zero if necessary
      var r = String(number);
      if ( r.length === 1 ) {
        r = '0' + r;
      }
      return r;
    }

    Date.prototype.toISOString = function() { // Return the date as a ISO string
      return this.getUTCFullYear()
        + '-' + pad( this.getUTCMonth() + 1 )
        + '-' + pad( this.getUTCDate() )
        + 'T' + pad( this.getUTCHours() )
        + ':' + pad( this.getUTCMinutes() )
        + ':' + pad( this.getUTCSeconds() )
        + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
        + 'Z';
    };

  }() );
}

      

Now you just need to run javascript.

0


source







All Articles