微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 toLocaleDateString().trim() 在日期中添加空格?

如何解决为什么 toLocaleDateString().trim() 在日期中添加空格?

嗨,我想向后端发送以下格式的日期字符串:dd/MM/yyyy,但是解析为字符串的方法 toLocaleDateString().trim() 正在该字符串中为我添加一个空格,就像您可以见:

error message

正在发送此字符串:'29/7/2021 '

在我的代码中,我有这个:

import React,{useState} from 'react';
import {usedispatch} from 'react-redux';
import {FormattedMessage} from 'react-intl';
import {useParams,useHistory} from 'react-router-dom';
import DatePicker from 'react-date-picker';

import {Errors} from '../../common';
import * as actions from '../actions';

const SetDateAndDiners = () => {

    const dispatch = usedispatch();
    const history = useHistory();
    const [reservationDate,setReservationDate] = useState(new Date());
    const [periodType,setPeriodType] = useState('LUNCH');      
    const [diners,setDiners] = useState(1);
    const [backendErrors,setBackendErrors] = useState(null);
    const {id} = useParams();
    let form;

    const handleSubmit = event => {

        event.preventDefault();

        if (form.checkValidity()) {

            var date = reservationDate.toLocaleDateString().trim();
            
            dispatch(actions.checkCapacity(
                id,date,periodType.trim(),diners,() => history.push(`/reservation/find-menu-products/${id}/${date}/${periodType}/${diners}`),errors => setBackendErrors(errors)
            ));

        } else {

            setBackendErrors(null);
            form.classList.add('was-validated');

        }
    }

    return (
        <div className="container">
            <div className="row justify-content-center" >
                <aside className="col-sm-6">
                    <Errors errors={backendErrors} onClose={() => setBackendErrors(null)}/>
                    <div className="card">
                        <article className="card-body">
                            <h4 className="card-title mb-4 mt-1">Establece la fecha en la que desea realizar una reserva y los comensales que asisitirán</h4>
                            <br/>
                            <form ref={node => form = node} className="needs-validation" 
                                novalidate onSubmit={e => handleSubmit(e)}>
                                <div className="form-group">
                                    <label htmlFor="reservationDate">Fecha: </label>
                                    <DatePicker
                                        cloSEOnScroll={true}
                                        isClearable
                                        placeholderText="dd/mm/yyyy"
                                        minDate={new Date()}
                                        dateFormat="dd/MM/yyyy"
                                        selected={reservationDate}
                                        onChange={(date) => setReservationDate(date)}
                                    />
                                    <div className="invalid-Feedback">
                                        <FormattedMessage id='project.global.validator.required'/>
                                    </div>
                                </div>
                                <div className="form-group">
                                    <label htmlFor="diners">Comensales: </label>
                                    <input type="number" id="diners" min="1" step ="1" className="form-control" 
                                        placeholder="Comensales"
                                        value={diners}
                                        onChange={e => setDiners(e.target.value)}
                                        autoFocus
                                        required/>
                                    <div className="invalid-Feedback">
                                        <FormattedMessage id='project.global.validator.required'/>
                                    </div>
                                </div> 
                                <div>
                                    <label htmlFor="discountType">Hora de reserva: </label>
                                    <div className="form-check-inline">
                                        <input type="radio" id="periodType" name="periodType" className="form-check-input" 
                                            value={'LUNCH'}
                                            onChange={e => setPeriodType(e.target.value)}
                                            autoFocus
                                            checked/>
                                        <label className="form-check-label" htmlFor="discountType">Comida</label>
                                    </div>
                                    <div className="form-check-inline">
                                        <input type="radio" id="periodType" name="periodType2" className="form-check-input" 
                                            value={'DINER'}
                                            onChange={e => setPeriodType(e.target.value)}
                                            autoFocus/>
                                        <label className="form-check-label" htmlFor="periodType2">Cena</label>
                                    </div>
                                </div>
                                <br/>
                                <div className="row">
                                    <div className="col">
                                        <div className="form-group">
                                            <button type="submit" className="btn btn-primary btn-block">Siguiente</button>
                                        </div> 
                                    </div>
                                </div>                                                              
                            </form>
                        </article>
                    </div>
                </aside> 
            </div>
        </div> 
    );

}

export default SetDateAndDiners;

我不知道为什么函数trim()没有删除空格

一个错误,它在初始化变量reservationDate 时不会对此产生影响,它是React DatePicker 没有在屏幕上显示日期,也没有显示修改的日期,但该变量确实与我之前的内容有所不同能够在屏幕上看到 console.log() 以及将空格添加到字符串的错误响应。我向您展示了我的意思的屏幕截图。

enter image description here

谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。