0

This is my data format:

 "21/03/2019 19:18"

The problem i am facing is, when ever if i am dealing with date or time there is an issue with the month ( it has 03 instead of 3 ). I am using library called date-fns. And also i have tried with the help of javascript date objects without using library, but no luck still the month should not have zero in-front of it.

So, how to remove the "0" in-front of "3", and one more problem is how to do this conditionally , because when its Dec, i will be getting data as "21/12/2019 19:18". So, in this case , i should not remove "1" as its located in same position of "0" in previous scenario.

In other words, i want to remove "0" by checking if there is "1" presented in that position or index, if presented then remove else remove "0"

How to achieve this.

I tried the below code:

 const d = new Date(2019,03,21)

But, its says legacy error. So when i removed "0" infront of "3" it works fine. Please help

Dazzile Pro
  • 253
  • 3
  • 13
  • That code snippet runs just fine for me, no errors. `new Date(2019,03,21)` and `new Date(2019,3,21)` (no leading zero) produce the exact same date object. – Chris Barr Mar 22 '19 at 17:35
  • Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected. – Ruan Mendes Mar 22 '19 at 17:37
  • Careful about your example date. The `Date` object in JavaScript uses numeric months 0-11, not 1-12. So `new Date(2019,03,21)` is *April* 21st. – Matt Johnson-Pint Mar 22 '19 at 17:59
  • The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes – Dazzile Pro Mar 22 '19 at 18:01
  • 1
    Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See https://stackoverflow.com/questions/37003770/why-javascript-treats-a-number-as-octal-if-it-has-a-leading-zero and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal – Matt Johnson-Pint Mar 22 '19 at 18:20

2 Answers2

0

I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?

we can use .split to break up the string into parts, and then we can use parseInt to convert some string parts into numbers. that will turn the string "03" into the number 3

function removeleadingZerosFromDateString(str) {
   //Break up the date string on the slashes and whitespace, so we have an array of all the parts
   var parts = str.split(/\/|\s/);
   console.log(parts);

   //Assign each array item to a variable so we can see what is what
   var day = parseInt(parts[0], 10);
   var month = parseInt(parts[1], 10);
   var year = parts[2];
   var time = parts[3];
   var meridian = parts[4];
 
   return day+'/'+month+'/'+year+' '+time+' '+meridian;
 }

var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
 console.log(result);
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
  • So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019" – Dazzile Pro Mar 22 '19 at 17:47
  • Careful - Months on the `Date` object are 0-11. You need to subtract one from the parsed value. – Matt Johnson-Pint Mar 22 '19 at 17:49
  • `parseInt` converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math `00000003` and `3 ` are equal. – Chris Barr Mar 22 '19 at 17:54
  • @MattJohnson true, but there's no requirement (that I could tell anyway) to use a `Date` object. They just want to remove leading zeros from a certain position in a string – Chris Barr Mar 22 '19 at 17:55
  • Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0. – Chris Barr Mar 22 '19 at 17:56
  • Sure. I was inferring that from the example in last part of question. But even there, they're making April 21, not March 21. – Matt Johnson-Pint Mar 22 '19 at 17:58
0

You said you were using date-fns, so I'll give an answer in that regard.

The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.

The syntax is:

var date = parse(dateString, formatString, baseDate, [options]);

See the documentation for the parse function in version 2.0.0-alpha.27.

In your case, it would be like this:

var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());

Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm").

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575