0

I have searched in SO and did not find my answer.

Here is my query , In javascript we generally generate time stamp like below

Date.now()

which outputs something like 1424592736769

But this value changes each time , since time is associated with it.

For example Suppose I generate timestamp today and store in database....after few days If I enter date in format like 2012, 01, 1 , I should be able to get the same time stamp.

What I tried

//got time stamp 
var a = Date.now() / 1000000 | 0
var d = Date(a);
console.log(d);

// and now based on the var a , I can be generate the timestamp based on specifying dateformat I know its wrong , some one guide me please.

P.S : I do this operation because I wanted to store a data on particular date as time stamp in database and modify those data later based on that particular date via timestamp.

For example my db should look like this

DAY         DATA
------------------
432324234 - 1000 rs //day1 data
767677657 - 2000 rs //day2 data

P.s : no one understood my question , To be more clear

I need to get todays date for example (feb 22 2015) as time stamp , After few days if I use Date(2015,01,22) , I must get the same time stamp generated on feb 22 2015

Vishnu
  • 2,372
  • 6
  • 36
  • 58

2 Answers2

1

From how I'm understanding what you're trying to do, getting rid of the new keyword like so will set d to a Date object of timestamp a:

//got time stamp 
var a = Date.now() / 1000000 | 0
var d = Date(a);
console.log(d);

EDIT Per this article: convert date to timestamp in javascript? to get a timestamp from a date do this:

var timestamp = new Date(myDate).getTime();
Community
  • 1
  • 1
winhowes
  • 7,845
  • 5
  • 28
  • 39
  • Do you just want the timestamp outputted? In that case just `d=a;` – winhowes Feb 22 '15 at 08:26
  • please see my P.s Section , Suppose I generate timestamp today and store in database....after few days If I enter date in format like 2012, 01, 1 , I should be able to get the same time stamp – Vishnu Feb 22 '15 at 08:29
  • Hey yea , I guess your edit is the way to go , Btw is this timestamp generated is global ? or change via location ? – Vishnu Feb 22 '15 at 08:45
  • Universal time, so global. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime – winhowes Feb 22 '15 at 08:47
1
a = new Date(Date.now());
d = new Date(a.getFullYear(), a.getMonth(), a.getDate()).getTime();

I need to get todays date for example (feb 22 2015) as time stamp , After few days if I use Date(2015,22,2) , I must get the same time stamp generated on feb 22 2015

Be careful that the constructor format is Date(yyyy,mm,dd) so for today: Date(2015,02,22).

Clemens Frahnow
  • 307
  • 1
  • 5
  • I need to get todays date(feb 22 2015) as time stamp , After few days if I use Date(2015,22,2) , I must get the same time stamp generated on feb 22 2015 – Vishnu Feb 22 '15 at 08:34
  • Ah, do you want to have access just with the date, and your problem is, that you also would have to know hour, minute and second, since this is also stored in the number? If this is your problem I suggest, that you use the link I gave you and save the day, the month and the year in the database, not the large number. – Clemens Frahnow Feb 22 '15 at 08:37
  • This stores just the current date always with time 00:00:00 in d. (22.02.2015 00:00:00). So you can find it with Date(2015,22,2). d = new Date(); a = new Date(Date.now()); d.setFullYear(a.getFullYear(), a.getMonth(), a.getDate()); – Clemens Frahnow Feb 22 '15 at 08:46
  • hey +1 for your effort , But when I run your code , It displays different timestamp each time...I am gonna accept winhouse answer – Vishnu Feb 22 '15 at 08:51
  • see my edit. You have to use Date(2015,02,22), not Date(2015,22,02). You always should get something like 'Sun Feb 22 2015 00:00:00 GMT-0800 (Pacific Normaltime (Mexico))' – Clemens Frahnow Feb 22 '15 at 08:53
  • Thats typo problem sorry...new Date(2015,01,22).getTime(); --> this is wat i need , this always prints same time stamp...but when i run ur code it always print different timestamp.. – Vishnu Feb 22 '15 at 08:59
  • Ok, now I see what you needed. Save d from my post in the database, and to read it, search for Date(2015,02,22).getTime(). Of course date number in my code alwys changes, time is running... =) – Clemens Frahnow Feb 22 '15 at 09:02
  • yea time is running in your code , I wanted without time , just date :) thanks for your help – Vishnu Feb 22 '15 at 09:06