Chempooro
Chempooro

Reputation: 435

convert only time String to Timestamp

So what I have is a time string which shows the time as h:m:s.ms But the problem is that I want to covert them to timestamp values it shows NaN values.

I am using Date.parse() to convert the time into timestamp.

Here is the code that I have tried.

var date;
function myFunction() {
  var d = new Date();
  var h = addZero(d.getHours(), 2);
  var m = addZero(d.getMinutes(), 2);
  var s = addZero(d.getSeconds(), 2);
  var ms = addZero(d.getMilliseconds(), 3);
  var maindate = h + ":" + m + ":" + s + "." + ms ;
  var datestring = Date.parse(maindate)
  var data = Math.random(0,1); 
  console.log("Date : ", maindate) ;
  console.log("Data : ", data);
 }
  myFunction();

You can see the date and data in the console window.

the date variable here shows NaN Value.

Please tell me what I am doing wrong.

Upvotes: 2

Views: 1343

Answers (5)

mohitesachin217
mohitesachin217

Reputation: 461

One way to parse only time in string format to date date object is as shown here.

var time = "14:43:30";

var date = new Date();
date.setHours(time.substring(0,2));
date.setMinutes(time.substring(3,5));
date.setSeconds(time.substring(6,8));
console.log(date);

Then you can convert it to timestamp like this.

var timestamp = date.getTime();

Here you can give time in string format and convert it into date.

In momentjs you can convert is as follows

var timestamp = moment(time, "HH:mm:ss").unix();

Upvotes: 0

Maximilian Fixl
Maximilian Fixl

Reputation: 688

For this, you don't need your addZero() function any more and it's unnecessary to delacre var date; globally.

var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var ms = d.getMilliseconds();
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();  

var maindate = day + '-' + month + '-' + year + ' ' + h + ':' + m + ':' + s + '.' + ms;
var datestring = Date.parse(maindate);

console.log("Data : ", datestring);

Take a look at momentjs.com, maybe this could be a clean and simple solution for you too - depending on your environment.

Upvotes: 0

Chempooro
Chempooro

Reputation: 435

As I want to get the timestamp in result. I got my sholution of the above question that I posted

Here is the Final code which is giving me the expected result.

  function addZero(x,n) {
  while (x.toString().length < n) {
    x = "0" + x;
  }
  return x;
}

var date;
function myFunction() {
  var d = new Date();
  var day = d.getDate();
  var month = d.getMonth() + 1; // Since getMonth() returns month from 0-11 not 1-12
  var year = d.getFullYear();
  var h = addZero(d.getHours(), 2);
  var m = addZero(d.getMinutes(), 2);
  var s = addZero(d.getSeconds(), 2);
  var ms = addZero(d.getMilliseconds(), 3);
  var maindate = year +"-" + day + "-" + month +" "+ h + ":" + m + ":" + s + "." + ms ;
   var datestring = Date.parse(maindate)
  var data = Math.random(0,1); 
  console.log("Date : ", datestring) ;
  console.log("Data : ", data);
 }

    myFunction();

I had to include the day month and year value also. WHich I updated in the Answer. rest of the code works fine.

Upvotes: 0

Mickael B.
Mickael B.

Reputation: 5205

If you want a timestamp you need a full time with day, month and year

var date;

function myFunction() {
  var d = new Date();
  var h = addZero(d.getHours(), 2);
  var m = addZero(d.getMinutes(), 2);
  var s = addZero(d.getSeconds(), 2);
  var ms = addZero(d.getMilliseconds(), 3);

  var day = d.getDate();
  var month = d.getMonth() + 1; // getMonth returns an integer between 0 and 11
  var year = d.getFullYear();  

  var maindate = `${day}-${month}-${year} ${h}:${m}:${s}.${ms}`;
  var datestring = Date.parse(maindate)
  console.log("Data : ", datestring);
 }
  myFunction();

Upvotes: 1

Julian
Julian

Reputation: 934

Parsing dates is a pain in JavaScript as there's no extensive native support. However you could do something like the following by relying on the Date(year, month, day [, hour, minute, second, millisecond]) constructor signature of the Date object.

var dateString = '17-09-2013 10:08',
    dateTimeParts = dateString.split(' '),
    timeParts = dateTimeParts[1].split(':'),
    dateParts = dateTimeParts[0].split('-'),
    date;

date = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);

console.log(date.getTime()); //1379426880000
console.log(date); //Tue Sep 17 2013 10:08:00 GMT-0400

You could also use a regular expression with capturing groups to parse the date string in one line.

var dateParts = '17-09-2013 10:08'.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/);

console.log(dateParts); // ["17-09-2013 10:08", "17", "09", "2013", "10", "08"]

Upvotes: 0

Related Questions