Converting String Date in React/Javascript

Converting string date in react/javascript

You should be able to do this purely with JavaScript, including parsing with Date() and, depending on your browser support requirements, format with toLocaleDateString().

The second example (commented out) just shows how it can work with a specific locale (as a string, though that can be an array of such strings).

function formatDate(string){    var options = { year: 'numeric', month: 'long', day: 'numeric' };    return new Date(string).toLocaleDateString([],options);}var dateString = "2018-05-18T04:00:00.000Z"document.getElementById("results").innerHTML = formatDate(dateString); // You could also provide specific locale, if needed. For example://function formatDate(string){//    var options = { year: 'numeric', month: 'long', day: 'numeric' };//    return new Date(string).toLocaleDateString('en-US',options);//}
<div id="results"</div>

Convert string to date in map loop - ReactJS

Use moment.js. You can able to convert a date string to any desired date format. For that you need to know the current date format of a date string

import moment from "moment";    
let resultDate = '2020-06-15 17:44:18'; // which is YYYY-MM-DD hh:mm:ss

<span>{moment(resultDate, 'YYYY-MM-DD hh:mm:ss').format('MM-DD-YYYY')}</span> // It will return 06-15-2020

how to convert date object to string using javascript

Not sure if you wanted it converted to UTC, but based on your example it looks like not.

Maybe something like this? Just replace "new Date()" with your date object.

const date = new Date(new Date() - 1000 * 60 * 60 * 7); // subtract 7 hours since .toISOString() is going to add 7 hours
console.log(date.toISOString().replace('Z','+0000'));

* I should add this assumes the client's timezone is Pacific Daylight Time, or a least something with the same offset

convert dateTime format in react js

You can do it as follows:

new Date('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.toISOString().split('.')[0]

=> '2021-08-23T10:33:00'

If you don't prefer the native way of converting it you can use the library Moment.js.

Your code would look as follows:

moment('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.format('YYYY-MM-DDTHH:mm:ss');

=> '2021-08-23T10:33:00'

If you don't want to keep the hours, minutes and seconds these examples will work.

Native way:

new Date('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)').toISOString().split('T')[0] + 'T00:00:00'

=> '2021-08-23T10:33:00'

With Moment.js:

moment('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.format('YYYY-MM-DDT00:00:00');

=> '2021-08-23T10:33:00'

Edit

As RobG mentioned in the comments, the toISOString() function will return the UTC time format. So even one more reason to use moment.js!

Formatting a Date String in React Native

The beauty of the React Native is that it supports lots of JS libraries like Moment.js.
Using moment.js would be a better/easier way to handle date/time instead coding from scratch

just run this in the terminal (yarn add moment also works if using React's built-in package manager):

npm install moment --save

And in your React Native js page:

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
}

You may check the moment.js official docs here
https://momentjs.com/docs/



Related Topics



Leave a reply



Submit