Jump to content

time into array


jimfog
 Share

Recommended Posts

I have managed(by using a function) to get the time in hours and minutes.Is there a way that the time can be converted into an array with elements the digits of the time.For example:If the time is 20:53, i would like this be converted into an array with the following elements:time=[2,0,:5,3,]What do you think?

Link to comment
Share on other sites

A bit awkward. Wouldn't it be easier to construct the array before/while you construct this string?AnywayIf you can guarantee leading zeroes when the hour/minutes < 10, this would work:

var a = t.split("");a.splice(2,1);a[2] = ":" + a[2];

Link to comment
Share on other sites

I have managed(by using a function) to get the time in hours and minutes.Is there a way that the time can be converted into an array with elements the digits of the time.For example:If the time is 20:53, i would like this be converted into an array with the following elements:time=[2,0,:5,3,]What do you think?
any particular reason for that implementation? There might be an easier way to save dates if you plan on doing stuff with them. That format may not be very consistent.
Link to comment
Share on other sites

A bit awkward. Wouldn't it be easier to construct the array before/while you construct this string?AnywayIf you can guarantee leading zeroes when the hour/minutes < 10, this would work:
var a = t.split("");a.splice(2,1);a[2] = ":" + a[2];

Ι am relatively new to javascript, that is why my methods may seem a little strange, i am still trying to get a grip on the language.Now regarding the issue per se.Behind the question of converting the time into array lies the attempt to convert the time into a number.I thought that to accomplish that i had to convert the time into an array.Instead i used the following code to convert the hour(which is a string) into a number.
y=x.slice(0,2);//Converting the hour string to a numberd=+y;

I suppose(as you have more experience than me), you do understand the code above.I wanted to convert the hour into a number so that i can manipulate it-add,subtract.The x variable above is where the time string is stored.Thanks for the help.

Link to comment
Share on other sites

yeah, if you're going to be doing addition/subtraction/comparisons with dates, you should ideally be using timestamps. When it comes time to visually represent that on the screen in a 'pretty' format, then you could use more features of the date object to represent the days, hours, month, etc of a given timestamp.

Link to comment
Share on other sites

think it would be best in the long run to rethink the approach.JavaScript Date Object referenceI'm guessing that you're starting off with a string that represents the time "20:53" and you want to manipulate that as a time not a string.this code is only for illustration purposes, a couple notions.

var str='20:53'; // however you get to this point...var ar=str.split(':'); convert time string to array with 2 members [hours, minutes]ar[0]=parseInt(ar[0]); // convert the string type to a number ar[1]=parseInt(ar[1]);var t={hour:ar[0], minute:ar[1]}; // create a object with hour and minute properties// orvar dt=new Date(); // this will be the current system date/timedt.setHours(ar[0], ar[1], 0, 0); // set hour, minute, second, millisecond of the date object dt.getHours() // return the hour 0-23 as a numberdt.getMinutes() // return the minute 0-59 as a number

Check out the link above to the JS Date Object refeerence and try some of the 'Try it yourself' examples, can be an easy way to see how the code works and experiment with variations.

Link to comment
Share on other sites

not sure, but I think he's starting off with a string representation of a time, don't think he was explicit about the source of the data - I at least get the feeling we're not starting off with a date object-- really think he's looking for parseIntAlso, if you want to let us in on a bit more detail as to what you're hoping to accomplish we might be able to give better pointers

Edited by packrat
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...