Kumar
Kumar

Reputation: 325

How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?

I have Done this

    var d='dd/mm/yy hh:MM:ss';
    var d1=d.split(" ");
    var date=d1[0].split("/");
    var time=d1[1].split(":");
    var dd=date[0];
    var mm=date[1]-1;
    var yy=date[2];
    var hh=time[0];
    var min=time[1];
    var ss=time[2];
    var fromdt= new Date("20"+yy,mm-1,dd,hh,min,ss);

Is there Any way to do it using JQuery OR JavaScript?

Upvotes: 11

Views: 66808

Answers (8)

Leana
Leana

Reputation: 9

now you can use toLocaleTimeString to format date without using moment or any package. read more for example

console.log(new Date().toLocaleTimeString(
    'en-US', { second: '2-digit', minute: '2-digit', hour12: false }))

Upvotes: 0

Alex
Alex

Reputation: 1

Thanks, @sean-coley, your answer helped a lot, but it gives me changed (local) time with different timezone. I would also add Date.UTC to the new Date function so the result time will be the same as in input

let dtStr = "12/03/2010 09:55:35"

console.log(strToDate(dtStr));  // Fri Mar 12 2010 09:55:35

function strToDate(dtStr) {
  if (!dtStr) return null
  let dateParts = dtStr.split("/");
  let timeParts = dateParts[2].split(" ")[1].split(":");
  dateParts[2] = dateParts[2].split(" ")[0];
  // month is 0-based, that's why we need dataParts[1] - 1
  return dateObject = new Date(Date.UTC(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]));
}  

Upvotes: 0

Sean Coley
Sean Coley

Reputation: 153

This will work regardless of timezone for the format dd/mm/yyy hh:mm:ss only. It also does not rely on third party packages:

let dtStr = "12/03/2010 09:55:35"

console.log(strToDate(dtStr));  // Fri Mar 12 2010 09:55:35

function strToDate(dtStr) {
  if (!dtStr) return null
  let dateParts = dtStr.split("/");
  let timeParts = dateParts[2].split(" ")[1].split(":");
  dateParts[2] = dateParts[2].split(" ")[0];
  // month is 0-based, that's why we need dataParts[1] - 1
  return dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]);
}

Upvotes: 10

Palani Kumar
Palani Kumar

Reputation: 350

We can convert any local date format to datetime datatype using moment js.

Syntax:

moment('<local date value>','<local date format>').format('<expected convert format>')

Example:

moment('26/05/1986 00:00', 'DD/MM/YYYY HH:mm').format("MM/DD/YYYY HH:mm");

then the output will be 05/26/1986 00:00

Cheers

Upvotes: 6

Sachin I
Sachin I

Reputation: 1508

From your code it seems that you are trying to convert string into date and also you are trying to fetch previous month. If yes then you can reconstruct your code as below:

Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
return d;
}

$(document).ready(function() {
     var dateString='2015-06-17T18:30:12';
    var d = new Date(dateString);
    alert(d.SubtractMonth(1));
});

Upvotes: 0

ANR Upgraded Version
ANR Upgraded Version

Reputation: 949

If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.

example:

  var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');

Upvotes: 14

PhilVarg
PhilVarg

Reputation: 4820

Date#parse should be able to parse that if you split on a string. Id also recommend looking into the npm package moment for date manipulation

Upvotes: 0

marekful
marekful

Reputation: 15351

How about Date.parse()?

new Date( Date.parse("05/12/05 11:11:11") );
// Thu May 12 2005 11:11:11 GMT+0200 (CEST)

The output produced is in local timezone and will differ in browsers in different timezones.

Upvotes: 7

Related Questions