3

How to convert my date to timestamp i dont know where im doing wrong.Any suggestion please ?

var dates = '27-04-2015';
var date1 = new Date(dates).getTime();
alert(date1);
2
  • console.log(+(new Date(dates))); Commented May 13, 2015 at 8:25
  • Parameter passed to new Date() is invalid Commented May 13, 2015 at 8:26

2 Answers 2

6

Parameter you passed in the new Date('27-04-2015') is not valid. Use sepeartor '/' for change date format and send to the new Date('27/04/2015').

var dates = '27-04-2015';
var dates1 = dates.split("-");
var newDate = dates1[1]+"/"+dates1[0]+"/"+dates1[2];
alert(new Date(newDate).getTime());

Working Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Date constructor takes argument in mm-dd-yyyy format, try this

var timeStamp = function(str) {
  return new Date(str.replace(/^(\d{2}\-)(\d{2}\-)(\d{4})$/,
    '$2$1$3')).getTime();
};
alert(timeStamp('27-04-2015'));

Here, I had used RegExp to swap the format from dd-mm-yyyy to mm-dd-yyyy. So '27-04-2015' gives '04-27-2015'. In the expression (\d{2}\-)(\d{2}\-)(\d{4}), \d denotes integer, {} for length, () for grouping, \ to escape special characters like -.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.