Home > Software design >  Parse the datestring which contain 0000 through moment Js
Parse the datestring which contain 0000 through moment Js

Time:11-10

If DateString value is "19780000". Then display only year like "1978" in UI page through moment.js

If DateString value is "19781100". Then display month and year like "Nov 1978" in UI page through moment.js

If it is not achieved by MOMENT.js then how can it do by javascript.

Kindly help me. Thanks in advance

CodePudding user response:

Here's a simple function to generate a string. It assumes the year portion of the string will be valid.

function dateStringToUI(ds) {
  const months = [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  let uiString = "";
  if (ds.length == 8) { 
    let yearPart = ds.substr(0, 4);
    let monthPart = months[parseInt(ds.substr(4, 2))];
    uiString = (monthPart !== null) ? `${monthPart} ${yearPart}` : yearPart; 
  }
  return uiString;
}
  • Related