获取提交后以特定格式显示的antd日期选择器数值。

huangapple go评论68阅读模式
英文:

how to get antd datePicker value in format after submission

问题

import { Button, Form, Input, DatePicker } from 'antd';
const App = () => {
  const onFinish = (value) => {
    console.log(value);
  };
  return (
    <Form name="form_item_path" layout="vertical" onFinish={onFinish}>
      <Form.Item name="firstName" label="First Name">
        <Input />
      </Form.Item>
      <Form.Item name="start-date">
        <DatePicker className='input w-full' format="YYYY-MM-DD HH:mm:ss" />
      </Form.Item>
      <Button type="primary" htmlType="submit">
        Submit
      </Button>
    </Form>
  );
};
export default App;

<details>
<summary>英文:</summary>

&gt;i build a form with Ant design,there is a datepicker input and i need to select 
     date in a specific date format but when i press submit the date send without any 
     format. how can i get the date on format after submit the form data

  import { Button, Form, Input, DatePicker } from &#39;antd&#39;;
    const App = () =&gt; {
      const onFinish = (value) =&gt; {
         console.log(value);
       };
   return (
      &lt;Form name=&quot;form_item_path&quot; layout=&quot;vertical&quot; onFinish={onFinish}&gt;
         &lt;Form.Item name=&quot;firstName&quot; label=&quot;First Name&quot;&gt;
            &lt;Input /&gt;
         &lt;/Form.Item&gt;
      &lt;Form.Item name=&quot;start-date&quot;&gt;
        &lt;DatePicker className=&#39;input w-full&#39; format=&quot;YYYY-MM-DD HH:mm:ss&quot;/&gt;
      &lt;/Form.Item&gt;
      &lt;Button type=&quot;primary&quot; htmlType=&quot;submit&quot;&gt;
         Submit
      &lt;/Button&gt;
   &lt;/Form&gt;
  );
};
export default App;

</details>


# 答案1
**得分**: 3

这是您要翻译的部分:

```javascript
startdate: value["start-date"].format("YYYY-MM-DD HH:mm:ss")

App.jsx:

import { Button, Form, Input, DatePicker } from "antd";
import "./index.css";
import "antd/dist/antd.css";
const App = () => {

  const onFinish = (value) => {
    let user = {
      firstname: value.firstName,
      startdate: value["start-date"].format("YYYY-MM-DD HH:mm:ss")  //在这里添加您所需的日期格式
    };
    console.log("Date in proper format", user);
  };

  return (
    <Form name="form_item_path" layout="vertical" onFinish={onFinish}>
      <Form.Item name="firstName" label="First Name">
        <Input />
      </Form.Item>
      <Form.Item name="start-date">
        <DatePicker className="input w-full" format="YYYY-MM-DD HH:mm:ss" />
      </Form.Item>
      <Button type="primary" htmlType="submit">
        Submit
      </Button>
    </Form>
  );
};
export default App;

请注意,我已经保留了代码中的引号和注释,没有翻译。

英文:

Check the following example

you need to convert it to required format using

startdate: value[&quot;start-date&quot;].format(&quot;YYYY-MM-DD HH:mm:ss&quot;)

App.jsx
<!-- language: lang-js -->

import { Button, Form, Input, DatePicker } from &quot;antd&quot;;
import &quot;./index.css&quot;;
import &quot;antd/dist/antd.css&quot;;
const App = () =&gt; {

  const onFinish = (value) =&gt; {
    let user = {
      firstname: value.firstName,
      startdate: value[&quot;start-date&quot;].format(&quot;YYYY-MM-DD HH:mm:ss&quot;)  //Add your required date format here
    };
    console.log(&quot;Date in proper format&quot;, user);
  };

  return (
    &lt;Form name=&quot;form_item_path&quot; layout=&quot;vertical&quot; onFinish={onFinish}&gt;
      &lt;Form.Item name=&quot;firstName&quot; label=&quot;First Name&quot;&gt;
        &lt;Input /&gt;
      &lt;/Form.Item&gt;
      &lt;Form.Item name=&quot;start-date&quot;&gt;
        &lt;DatePicker className=&quot;input w-full&quot; format=&quot;YYYY-MM-DD HH:mm:ss&quot; /&gt;
      &lt;/Form.Item&gt;
      &lt;Button type=&quot;primary&quot; htmlType=&quot;submit&quot;&gt;
        Submit
      &lt;/Button&gt;
    &lt;/Form&gt;
  );
};
export default App;

<!-- end snippet -->

Output:

获取提交后以特定格式显示的antd日期选择器数值。

huangapple
  • 本文由 发表于 2023年2月14日 05:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定