Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9313447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:53:05+00:00 2026-06-19T01:53:05+00:00

On a project I’m currently working on in JavaScript, I’m using decimal formats so

  • 0

On a project I’m currently working on in JavaScript, I’m using decimal formats so it’s easier to calculate with rather than using an hour/minute format in strings (calendar related project). To display the time on the user’s screen though, the timecode has to be shown as hh:mm.

I thought it would be great to use a String prototype function for this as it would allow me to use code like:

var time = 8.75;
document.write("Meeting at "+time.toTime()); // writes: Meeting at 8:45

So far, I’ve got that almost working, using:

String.prototype.toTime = function(){
    var hrs = this.toString().slice(0,this.indexOf("."));
    var min = Math.round(this.toString().slice(this.indexOf("."))/100*60);
    min = min<10 ? "0"+min : min.toString();
    return hrs+":"+min;
}

The problem, though, is that this will only work if the variable time is a string. Otherwise it will give an undefined error.

Would there be any way of applying the prototype to a different object in JavaScript, so that I don’t have to use time.toString().toTime()?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:53:06+00:00Added an answer on June 19, 2026 at 1:53 am

    Firstly, you can add to the Number prototype. Many people will warn against modifying prototypes, which in many cases is justified. If there is a chance 3rd party scripts will be running alongside yours, it is a danger to modify prototypes.

    Secondly, I simplified your code a little, using modulus, and floor to calculate the hrs and mins…

    Number.prototype.toTime = function(){
      var hrs = Math.floor(this)
      var min = Math.round(this%1*60)
      min = min<10 ? "0"+min : min.toString();
      return hrs+":"+min;
    }
    
    var time = 8.25;
    console.log("Meeting at "+time.toTime());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The project I am currently working on has, in the checkout, an option to
The project that I am working on (Node.js) implies lots of operations with the
The project currently has a UIviewController called Dashboard that acts as the main view
This project is in Obj-C for iphone. I'm using the double float version of
Project I'm working on uses jQuery. I have a series of Ajax calls being
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on
Project was working on PC number 1. Copied project to external drive and trying
Our project is using many static libraries to build the application. How can we
The project I'm working on has an Oracle backend, and we use SQL Navigator
project: Using VB.NET to build a winforms database interface and work-automation app. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.