Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date from one format to another format in JavaScript

I have a string of a date in javascript in format #1. I need to convert it to format #2.

The problem starts when one format is "dd/mm/yy" and the other is "mm/dd/yy".

The formats change dynamically and I have the formats as strings, but I need a function like

   Date newDate = convert(currentDate, currentFormatString, newFormatString).

How can I do it?

like image 977
TamarG Avatar asked Oct 22 '14 05:10

TamarG


People also ask

How do I format a date in JavaScript?

The toDateString() Method in JavaScript First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary. Four digit year (at least), padded on the left with zeros if necessary.

How do I format a date from one form to another in HTML?

Date newDate = convert(currentDate, currentFormatString, newFormatString).

How convert YYYY MM DD string to date in JavaScript?

To convert a dd/mm/yyyy string to a date:Split the string on each forward slash to get the day, month and year. Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.


2 Answers

You should look into momentjs, which is a javascript date/time library. With that, you can easily convert between dates of different format. In your case, it would be:

string newDate = moment(currentDate, currentFormatString).format(newFormatString)

For example, moment("21/10/14", "DD/MM/YY").format("MM/DD/YY") would return "10/21/14"

like image 98
trekforever Avatar answered Oct 08 '22 05:10

trekforever


You can use the function below

console.log(changeDateFormat('12/1/2020','dd/MM/yyyy','MM/dd/yyyy'));

function changeDateFormat(value, inputFormat, outputFormat) {

let outputSplitter = "/";
let strOutputFormat = outputFormat.split(outputSplitter).map(i => i.toUpperCase());
if (strOutputFormat.length != 3) {
  strOutputFormat = outputFormat.split('-');
  outputSplitter = '-';
}

if (strOutputFormat.length != 3) throw new Error('wrong output format splitter :(');

let date = null;

if (value instanceof Date) {
  date = {
    ["YYYY"]: value.getUTCFullYear(),
    ["MM"]: value.getMonth() + 1,
    ["DD"]: value.getDate()
  }
}

if (typeof value == 'string') {
  let inputSplitter = "/";

  var strInputFormat = inputFormat.split(inputSplitter).map(i => i.toUpperCase());
  if (strInputFormat.length != 3) {
    strInputFormat = inputFormat.split('-');
    inputSplitter = '-';
  }

  if (strInputFormat.length != 3) throw new Error('wrong input format splitter :(');

  let dateElements = value.split(inputSplitter);
  if (dateElements.length != 3) throw new Error('wrong value :(');

  date = {
    [strInputFormat[0]]: dateElements[0],
    [strInputFormat[1]]: dateElements[1],
    [strInputFormat[2]]: dateElements[2],
  }
}

if (!date) throw new Error('unsupported value type:(');

let result = date[strOutputFormat[0]] + outputSplitter
  + date[strOutputFormat[1]] + outputSplitter 
  + date[strOutputFormat[2]];

return result;
 }
like image 23
Osama AbuSitta Avatar answered Oct 08 '22 06:10

Osama AbuSitta