var str = '2011-04-11T10:20:30Z'; var date = moment(str); var dateComponent = date.utc().format('YYYY-MM-DD'); var timeComponent = date.utc().format('HH:mm:ss'); console.log(dateComponent); console.log(timeComponent);
Or simply with string manipulation
var str = '2011-04-11T10:20:30Z'; var parts = str.slice(0, -1).split('T'); var dateComponent = parts; var timeComponent = parts; console.log(dateComponent); console.log(timeComponent);
There's two parts to the moment operation: reading the date/time in, and spitting it back out. You've got the first part:
but then you need to call an output function, eg:
moment("2011-04-11T10:20:30Z").format('YYYY-MM-DD h:mm:ss a')
EDIT: However it's worth noting that Moment itself is a deprecated library, and even its authors will recommend switching to a smaller competitor: https://momentjs.com/docs/
- How to get time format to HH:mm using moment js
- How do I specify the timezone and date object format on moment correct such that I do not get Invalid date and a moment is deprecated warning?
- Formatting date and time using Moment with React JS
- How to filter both date and time together from list using reactjs
- How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js?
- How can I format time using moment.js in React JS and webpack
- How to change date format and time zone with DatePicker(moment.js) in React(Ant Design v4)?
- Merging a date and time into one moment object to compare against today
- How to format a moment date and compare with another moment date
- Can't format moment object to date and time
- How to get current time in hh:mm:ss format using moment in React JS
- How can I correctly render the correct date format into a table using react?
- How to convert moment date to a string and remove the moment object
- How to do POST in FORM Submit using reactjs and pass the object value into REST service?
- How to show time and date in realtime in React JS?
- How to format time in React SSR rendered page using client time zone?
- How do I create a moment object with just the time and not the date?
- How to persist to both localStorage and sessionStorage at the same time using redux-persist?
More Query from same tag
- Typing a custom useRef hook for any type of HTML element
- Set focus in input on clicking div
- pagination from API react how to detect last item
- I have to click on my link twice before state is update and styling is changed
- Globally mock a function in Jest/Enzyme in React
- Using image files with webpack 5 and react, webpack is processing the image but it won't load in the browser
- Element: type is invalid Error
- ReactJS image not displaying when src is passed in from parent
- FlowRouter - Inserting content to HTML template
- Update a single property of object from array using useState() hook
- Multiple components from object
- How to write ReactJS function component without using JSX?
- How can a function correctly accept a React component and its props as arguments in TypeScript and render this component?
- Passing data between components React-JS
- Combining filter and includes to filter multiple values
- Comparing object value with others in same array
- click button to route to next page
- React.js - How to stop setInterval and resume it after click
- How to style body tag with CSS-in-JS approach?
- How to format all my vscode files from 4 space indentation to 2 spaces?
- Reactjs Array.slice(start, end) not working
- ReferenceError: ace is not defined
- Material-table TypeError: Cannot add property tableData, object is not extensible
- How do I disable a fieldsset using a variable instead of the "disable" word
- React, how to access child ref in parent
- Error: [Header] is not a <Route> component
- How to verify the username in the url
- How to set props to state in component in my case?
- How to get url from popup?