1

INTRODUCTION

I have a DatePicker component that returns a JS Native Date Object. When the user selects a date, I automatically convert it to a locale date string, just for making it visible in a text input. Every single text input in my app has a function 'getText()' to get their values... so I need to get this locale date string and convert it back to a native js Date object.

This has to work for every country.

Here is a representation of what I am trying to do:

enter image description here

PROBLEM

I have tried to do new Date(localeDateStr) but doesn't work as expected because the Date object doesn't accept date formats like "DD/MM/YYYY". Any ideas how to handle this?

I would really appreciate your help. Thank you.

Pd: I am using js-joda and native date methods from JS. Also, the react framework.

JS-Joda has a method "LocaleDate.parse(dateStr)" which accept string of the type 'mm-dd-yyyy' so it doesn't work for my use case.

Victor Molina
  • 2,353
  • 2
  • 19
  • 49

2 Answers2

1

The d object could store both values that can be retrieved by toString() and valueOf() methods, something like:

d = {
toString(): d.toLocaleDateString(),
valueOf(): d.getTime()
}

For representation / render purpose - toString() will be called implicit, for mathematical - valueOf(). You could e.g multiply d by 1 before sending through API

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
  • So, you mean to render the toLocaleDateString() in the textinput for render purpose, but for mathematical to use valueOf()? Will be the same as keeping the original d object (Date type) and move it to parent components? – Victor Molina Sep 04 '20 at 10:31
  • 1
    I don't know the structure of you app ... I'm using exactly that solution when I have to deal with date picker and API. JS method toString and valueOf are powerfull and devs sometimes don't remember that they exists - please compare https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf - object is treated as simple value - you need it for timestamp. I suggest not to split it up - one place for both values should be enough - if you split it - there will l be hard to synchronize multiple occurrence – Krzysztof Safjanowski Sep 04 '20 at 11:03
0

Convert LocaleDateString back to Date in JavaScript:

I have explained conversion without using moment.js in javascript

1.For string format,"DD/MM/YYYY"

String date="24/06/2022";
var arr = date.split("/");
let dateObj= new Date(parseInt(arr[2]);
parseInt(arr[1]),parseInt(arr[0])); //new Date(int year,int Month,int day)

2.For String Format , "MMM-yyyy"

String date="Aug-2021";  
var arr = reviewDate.split("-");  
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
let month = months.indexOf(arr[0].toLowerCase());
let dateObj= new Date(parseInt(arr[1]), month);

Below code works only to convert string which are in ISO 8601 standards like YYYY-MM-DD or YYYY/MM/DD else it prints as invalid date

let dateObj=new Date("");

schnawel007
  • 3,982
  • 3
  • 19
  • 27
Vidhya J
  • 1
  • 1