How to format a String to Date in React Native?

Amine

I am getting a Date as a String in this format from the server yyyyMMdd:hhmmss.

Is there a generic way to format this string to a Date object?

EDIT

 formatDate = (data) => {
    return data.slice(6, 8) + "." + data.slice(4, 6) + "." + data.slice(0, 4) + " " + data.slice(9, 11) + ":" + data.slice(11, 13)
}
RobG

The way you're reformatting the string is fine, even though it seems like a lot of code for a small job, slice is pretty fast. Some alternatives (not necessarily "better", just different):

// Reformat yyyyMMdd:hhmmss as dd.mm.yyyy hh:mm:ss
function formatMatch(s) {
  let b = s.match(/\d\d/g) || [];
  return `${b[3]}.${b[2]}.${b[0]}${b[1]} ${b[4]}:${b[5]}:${b[6]}`;
}

function formatReplace(s) {
  return s.replace(/(\d{4})(\d{2})(\d{2}):(\d{2})(\d{2})(\d{2})/, '$3.$2.$1 $4:$5:$6');
}

formatDate = (data) => {
    return data.slice(6, 8) + "." + data.slice(4, 6) + "." + data.slice(0, 4) + " " + data.slice(9, 11) + ":" + data.slice(11, 13)
}

let s = '20200323:123445';
console.log(formatDate(s));
console.log(formatMatch(s));
console.log(formatReplace(s));

If you want to get an actual Date object, then instead of using the bits to create another string, just pass them into the constructor:

// Parse yyyyMMdd:hhmmss to Date object
function parseD(s) {
  let b = s.match(/\d\d/g) || [];
  return new Date(b[0]+b[1], b[2]-1, b[3], b[4], b[5], b[6]);
}

let s = '20200327:134523';
console.log(parseD(s).toString());

The use of || [] means that if there's no match, an empty array is returned so all the b[*] terms return undefined and the result is an invalid date.

The above uses match, but slice or substring can be used the same way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Native: How to convert date format?

How to format ISO date in React-Native

How to format this string to a date format?

How to format string to date

Formatting a Date String in React Native

React Native New Date string

How to set date format of a date into Date but not String?

Need to Format Current Date in Specific Format in React Native using Libraries

How to set Date format as like How many Hours From Now in React native android?

How to format string into date sqlite

How to format date string in java?

how to format string to date on flutter?

how to customize react-native-date-picker value to 'yyyy'-'mm'-'dd' format

How to fix inaccurate date format and time of moment.js in react native?

Extract date from string - react native

How to format ''date+hour'' string into date time format

PHP date_format(): How to format date from string value

How to format date string if the given date format changes

How to parse JSON format date string into date format

How to format a date string if you do not know the format of date input?

How to convert a string date in european format to a valid mysql date format?

React Native - how to trim a string

how to String to React Native Component

React Native - Customize date format using Moment.js

How to format code of react native in atom

How to convert date of string format to another string format in swift(ios)

How to convert String date into ISO Date format date

How to format date in react-calendar?

React Native: How to save date in realm DB