0

I have tried to convert (Tue Jan 19 17:26:24 IST 2016) date into timestamp but the output is NaN. I want to add 24 hr in the specific time.

I have used JavaScript

var nowDate = new Date("Tue Jan 19 17:26:24 IST 2016");
    var date = nowDate.getTime();
var deadline = new Date(Date.parse(new Date(date)) + 1 * 24 * 60 * 60 * 1000);  

the deadline return NaN value.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
SR230
  • 253
  • 1
  • 4
  • 13
  • Before thinking of generating a Unix timestamp you need to obtain a Date object and your first line of code triggers "Invalid Date" when I run it in Node. – Álvaro González Jan 19 '16 at 09:07
  • 2
    Do you always have the same format (with year after time zone) or it's a free input? – Álvaro González Jan 19 '16 at 09:10
  • Have you tried valueOf()? http://www.w3schools.com/jsref/jsref_valueof_date.asp – kometen Jan 19 '16 at 09:11
  • What's the point of `Date.parse(new Date(date))`? Other than truncating milliseconds, it's equivalent to just `date`... – jcaron Jan 19 '16 at 09:16
  • Your last line doesn't make sense: new Date(date) is a Date object, but Date.parse expects a string. You don't need parsing on the other hand – Gavriel Jan 19 '16 at 09:16
  • I have fetch the date from my DataBase and I got the year after IST. – SR230 Jan 19 '16 at 09:22
  • @Gavriel ...I have done this because I want to covert date into epoch time . converting date(Tue Jan 19 17:26:24 IST 2016) to (1453195480) and then to add 24 hr in that timestamp. the next step I convert the whole timestamp to date. I have refer with this doc: http://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/ – SR230 Jan 19 '16 at 09:27
  • But 1. date is already timestamp, you don't need to convert it back to Date. 2. Date.parse expects a string, not a Date. See my answer, the whole mess is much shorter. And it gets back to only decide on the original date string format – Gavriel Jan 19 '16 at 09:29

2 Answers2

2

If you are able to get the date string in a standard format, you can just do like this. (There are other standard formats as well: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

var ts = Date.parse("2016-01-19T17:26:24"); // either this
var ts = Date.parse("Tue, 19 Jan 2016 17:26:24 (IST)"); // or this format
var deadline = new Date(t + 24*3600*1000);

Depending on what do you have in your database, you either need to get the date in a standard format, or if it's saved as string then you can cut it into pieces and put it back together in the correct format in my example

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • it work fine. but the date is hardcode over here. but in my date year is coming after time Zone . So how to change that date into the proper date ? – SR230 Jan 19 '16 at 09:33
  • It's not hardcoded, it' the same date you tried in your question, just in a different format. Another possible standard format is: Date.parse("Tue, 19 Jan 2016 17:26:24 (IST)") – Gavriel Jan 19 '16 at 09:37
1

I strongly recommend using a library for DateTime arithmetic such as the brilliant:

However, if you wish to get a UNIX valid timestamp you could use the following:

var Unix = Math.round((Date.parse(date/1000);

Alternatively, check out this stack overflow question.

Community
  • 1
  • 1
o-o-awake-o-o
  • 397
  • 1
  • 3
  • 18