1

hey how to convert string in timestamp

I recovered the text of time and I convert it in date

when I converted to timestamp the value is negative

var timenow=$("#time").text();
var res = timenow.split(":");
var d80 = new Date();
var curr_date = d80.getDate();
var curr_month = d80.getMonth();

var curr_year = d80.getFullYear();
var date1 = new Date(curr_date, curr_month, curr_year, res[0], res[1],res[2]);

console.log(date1);

const timestamp = date1 / 1000;
console.log(timestamp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="time">
00:50:30
</div>
Moob
  • 14,420
  • 1
  • 34
  • 47

3 Answers3

1

You're using the Date's constructor wrong. First parameter is the year:

var date1 = new Date(curr_year, curr_month, curr_date, res[0], res[1],res[2]);

Also, I think your code can be improved:

const [hours, minutes, seconds] = $("#time").text().split(":");

let currentDate = new Date();
currentDate.setHours(hours, minutes, seconds)

const timestamp = currentDate.getTime();
console.log(timestamp);
danielperaza
  • 412
  • 3
  • 12
0

In JavaScript, a timestamp is the amount of milliseconds elapsed since 1 January 1970 (1 January 1970 is also known as Unix epoch time)

In your sample code date1 is 1931-04-11. Some 39 years before the epoch! Hence the negative value.

You need to use the correct format when constructing date1:

var date1 = new Date(curr_year, curr_month, curr_date, res[0], res[1],res[2]);

EG:

var timenow=$("#time").text();
var res = timenow.split(":");
var d80 = new Date();
var curr_date = d80.getDate();
var curr_month = d80.getMonth();

var curr_year = d80.getFullYear();
//var date1 = new Date(curr_date, curr_month, curr_year, res[0], res[1],res[2]);

var date1 = new Date(curr_year, curr_month, curr_date, res[0], res[1],res[2]);
console.log(date1);

const timestamp = date1 / 1000;
console.log(timestamp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="time">
00:50:30
</div>
Moob
  • 14,420
  • 1
  • 34
  • 47
0

Call the date constructor in right way

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

More information: click here

var timenow=$("#time").text();
var res = timenow.split(":");
var d80 = new Date();
var curr_date = d80.getDate();
var curr_month = d80.getMonth();


var curr_year = d80.getFullYear();


var date1 = new Date(curr_year,curr_month, curr_date,res[0], res[1], res[2]);

console.log(date1); //"2019-10-26T01:20:00.000Z"


const timestamp = date1.getTime();// to get timestamp
console.log(timestamp);

let date2 = new Date(timestamp);
console.log(date2); //"2019-10-26T01:20:00.000Z"
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="time">
00:50:30
</div>
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
  • but when i converted timestamp in date i did not find the same result – Antoine coulibaly Oct 25 '19 at 17:19
  • You should get timestamp by using `date1.getTime()` not dividing it by 1000 – Akshay Bande Oct 25 '19 at 17:24
  • just I have last questions please how compare difference between 00:50:30 and anthoer variable for exemple 01:20:50 – Antoine coulibaly Oct 25 '19 at 18:50
  • If you want to compare time then you can't just compare 01:20:50 and 00:50:30 cause they are strings if you compare them they will get lexicographically compared giving the wrong answer maybe it will work but that is not the correct way to compare time. You have to convert them into `date` objects like I did date1 and date2 then you can compare them using comparison operators. https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Akshay Bande Oct 26 '19 at 02:28
  • There's a mistake the correct is var date1 = new Date(curr_year,curr_month, curr_date,res[0], res[1], res[2]); – Antoine coulibaly Oct 26 '19 at 10:07