0

I want to recognize a string value is date string or not in react. Because I want to change the format of date, if the value is a date string.

this is my code:

let value = //get a string value from other component (e.g. `2020-9-8 00:00:00` or `text`

value is date string // I don't know how to write this condition and check value is dateString
?
  moment(value,"YYYY-M-D HH:mm:ss").format("jYYYY/jM/jD HH:mm:ss");
:
  value;
Zahra Talebi
  • 675
  • 4
  • 11
  • 26
  • Does this answer your question? [Detecting an "invalid date" Date instance in JavaScript](https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript) – Abhi Sep 08 '20 at 10:02

3 Answers3

0

if you could install dateformat you can retrive the string in required format in many combinations.

  import dateFormat from 'dateformat';
  {dateFormat(this.state.CreatedOn, "mm/dd/yyyy")}

https://www.npmjs.com/package/dateformat

Pavan Kalyan
  • 377
  • 1
  • 10
  • Thank you. But My problem is not changing the format. The point is check `value` is date string or not. for example `value = "test"` is not a date string but `value = "2020-9-8 00:00:00"` is a date string. I want to check this. – Zahra Talebi Sep 08 '20 at 09:58
0

You can try Date.parse() , if it gives Nan it means that string is not a date

Ahmad Suddle
  • 190
  • 7
  • thank you. yes. it is a solution but do you know other solution? I mean, I have lots of value and with this solution, I should convert every single value to `Date` , then check if it is `Nan` or not. I was wondering if you could suggest me a more optimize solution. – Zahra Talebi Sep 08 '20 at 10:07
  • Maybe you can first check if the value has number , if it has numbers then only it can be a date and it will save time rather then checking every string as date – Ahmad Suddle Sep 08 '20 at 10:13
0

you may try using moment.js:

https://momentjs.com/

install package: npm install moment --save

use as below

import Moment from 'moment';

render(){
    Moment.locale('en');
    var dt = '2016-05-02T00:00:00';
    return(<View> {Moment(dt).format('d MMM')} </View>) //basically you can do all sorts of the formatting and others
}

else you can directly check as below:

const date = Moment("2018-05-18T04:00:00.000Z").format('DD MMM, YYYY');
console.log(date);

Example:

const date = Moment("2018-05-18T04:00:00.000Z").format('DD MMM, YYYY');
const dateString = Moment("Test").format('DD MMM, YYYY');
console.log(date); // Ans : 18 May, 2018
console.log(dateString); // Ans : Invalid date
  • if 'value = "test"' , how you can check the value is not date string? – Zahra Talebi Sep 08 '20 at 10:13
  • You will check like thay const date = Moment("2018-05-18T04:00:00.000Z").format('DD MMM, YYYY'); const dateString = Moment("Test").format('DD MMM, YYYY'); console.log(date); //18 May, 2018 console.log(dateString);//Invalid date – Pranali Gajjar Sep 08 '20 at 10:25
  • @ZahraTalebi You can check Example in ans. You will get Invalid date if value is string or not date. – Pranali Gajjar Sep 08 '20 at 10:28