Jump to content

altering UNIX timestamp


jimfog

Recommended Posts

Is there any js method to alter a UNIX timestamp?

 

All theses set methods seem not to have any effect on timestamps per se.

 

Suppose I have 1402376400.

 

How I can set it to 1402376100....

 

I can use simple arithmetic of course to do my job....but these would require some extra calculations.

 

The goal is this:

Given a timestamp...set the time in the beginning of the day.

 

Suppose I have in UNIX timestamp form this date: Tue, 10 Jun 2014 17:34:53 GMT

I want to convert it to this:Tue, 10 Jun 2014 01:00:00 GMT

 

Is there a js code/method available to help in this direction?

 

One issue(important to consider) is that the given timestamp varies and no standard calculation would be appropriate here,That is why I was asking if a js method existed for that.

Edited by jimfog
Link to comment
Share on other sites

The date constructor will accept values for month, day, year, hour, etc. ...

I do not understand how will this help in my case.

Can you be more specific?

Link to comment
Share on other sites

If you want to get a timestamp for Tue, 10 Jun 2014 01:00:00 GMT, then create a date object with those values and get the timestamp.

So,,,in other words...you are saying to convert the timestamp into time format like the above and then use this in the date constructor...which in turn will give a new timestamp(modified).

Link to comment
Share on other sites

he's saying, if you have the "pretty" date, then use the Date constructor with those values, and then call getTime on it.

 

If you need to figure out the difference between times, you can subtract one timestamp from the other.

Link to comment
Share on other sites

he's saying, if you have the "pretty" date, then use the Date constructor with those values, and then call getTime on it.

 

If you need to figure out the difference between times, you can subtract one timestamp from the other.

The above being said is it correct to assume I must set the "pretty date" at the beginning of the day.

I mean whatever calculation needs to be done must be done on the "pretty date",not in the UNIX timetamp.

Link to comment
Share on other sites

You "must" not do anything, you "can" do whatever you want. This is programming. You can pass a Unix timestamp to the date constructor and get a date for that time. You can use the various methods of the date object to return pieces of the date so that you can format the date however you want to display it. You can return a Unix timestamp from a date object to keep working with the timestamps. If you don't know how to use date objects then check the MDN reference to see a list of the properties and methods.

Link to comment
Share on other sites

You are right...let me do some testing on my own and I will get back...if necessary.

Link to comment
Share on other sites

I would suggest doing all your calculations with the unix timestamp. Since it's a number, it's a more practical approach for diffing, adding, subtracting, etc. Once your calculations are done, use Date to get a pretty date out of it. Also, you want to look into this library.

http://momentjs.com/

Link to comment
Share on other sites

I will look into the library you mentioned.

 

For now I have managed to do what I want...take a look at this fiddle: http://jsfiddle.net/fiddlehunt/ZP74r/

 

It might be convenient to make calculation when working with timestamps but I do not know if it is possible

to set the hour/minute(as shown in the fiddle) with these.

 

It is possible to set hour/min with UNIX timestamps-my fault I did not saw it earlier.

Edited by jimfog
Link to comment
Share on other sites

I have made a considerable progress in the direction I wish.

 

I have one small question though.

I use getMinutes to "grab" the minutes of a given timestamp.

 

Suppose we have 10:00-getMinutes will output 0,I want 00 though.

What can I do?

Add the second O my self with custom code?

Link to comment
Share on other sites

I have concluded to this code:

var minutes= j.getMinutes();var adjminuts = (minutes === 0) ? 00 : minutes;

The problem though is that when minutes === 0,adjminutes takes always the value 0,not 00 as I want to.

 

WHat can I do here? It seems that JS,when we have a number comprised of many zeros,than js will just print 0 in the browser.

Link to comment
Share on other sites

the issue I believe is that JavaScript will truncate the leading 0. Try this

var adjminuts = (minutes === 0) ? "00" : minutes;
Link to comment
Share on other sites

 

the issue I believe is that JavaScript will truncate the leading 0. Try this

var adjminuts = (minutes === 0) ? "00" : minutes;

Yes,that will do.Actually I had resort to this solution even before I see your post.

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
×
×
  • Create New...