{"id":32813,"date":"2024-11-01T09:11:44","date_gmt":"2024-11-01T09:11:44","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32813"},"modified":"2024-11-01T12:34:49","modified_gmt":"2024-11-01T12:34:49","slug":"doctype-react-course-date-object-and-dates","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32813\/","title":{"rendered":"React Course: Date Object and Dates"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Dates and times are particularly important in modern web applications. Various functionalities such as appointments, schedules, timelines, and countdowns depend on dates and times. In this lecture, we will take a closer look at the <strong>Date object<\/strong> in JavaScript and discuss how to effectively use this object in a React environment.\n    <\/p>\n<h2>What is the Date object?<\/h2>\n<p>\n        The <strong>Date object<\/strong> in JavaScript is a built-in object for representing and manipulating dates and times. It allows users to create, compare, and format dates and times in various formats.\n    <\/p>\n<h3>Creating a Date object<\/h3>\n<p>\n        The Date object can be created in various ways. The most basic method is to use the <code>new Date()<\/code> constructor. If no arguments are provided, the current date and time are returned.\n    <\/p>\n<pre><code>const now = new Date();<\/code><\/pre>\n<p>\n        You can also specify a particular date and time. The example below creates a Date object for October 1, 2023.\n    <\/p>\n<pre><code>const specificDate = new Date('2023-10-01T00:00:00');<\/code><\/pre>\n<h3>Formatting dates<\/h3>\n<p>\n        After creating a Date object, you can format the date and time using various methods. The <code>toLocaleDateString()<\/code> and <code>toLocaleTimeString()<\/code> methods can convert the date and time into a format that matches the user&#8217;s locale.\n    <\/p>\n<pre><code>const options = { year: 'numeric', month: 'long', day: 'numeric' };\nconsole.log(now.toLocaleDateString('en-US', options)); \/\/ October 1, 2023\n<\/code><\/pre>\n<h2>Using the Date object in React<\/h2>\n<p>\n        When using the Date object in a React app, it can be easily utilized in state-managing components. For example, let&#8217;s create a simple component that displays the user&#8217;s current date and time.\n    <\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\n\nconst CurrentDateTime = () =&gt; {\n    const [dateTime, setDateTime] = useState(new Date());\n\n    useEffect(() =&gt; {\n        const timerID = setInterval(() =&gt; {\n            setDateTime(new Date());\n        }, 1000);\n\n        return () =&gt; clearInterval(timerID);\n    }, []);\n\n    return (\n        <div>\n            <h2>Current Date and Time: {dateTime.toLocaleString('en-US')}<\/h2>\n        <\/div>\n    );\n};\n\nexport default CurrentDateTime;<\/code><\/pre>\n<h3>State Management and React Hooks<\/h3>\n<p>\n        In the example above, the <code>useState<\/code> hook is used to manage state, and the <code>useEffect<\/code> hook is set up to update the current time every second whenever the component mounts. This way, users can see the current time in real-time.\n    <\/p>\n<h2>Comparing dates<\/h2>\n<p>\n        You can compare Date objects to determine if a specific date is in the past, present, or future. The Date object can be compared numerically based on the time. When comparing two Date objects, it can be done as follows.\n    <\/p>\n<pre><code>const date1 = new Date('2023-10-01');\nconst date2 = new Date('2023-11-01');\n\nif (date1 &lt; date2) {\n    console.log('date1 is earlier than date2.');\n} else {\n    console.log('date1 is later than date2.');\n}<\/code><\/pre>\n<h2>React and Date Libraries<\/h2>\n<p>\n        In addition to the basic functionalities of the Date object in React projects, various libraries can be used to handle dates and times more easily. Prominent examples include <strong>moment.js<\/strong>, <strong>date-fns<\/strong>, and <strong>day.js<\/strong>.\n    <\/p>\n<h3>Using Moment.js<\/h3>\n<p>\n<strong>Moment.js<\/strong> is a popular library that helps manipulate dates and times easily. After installation, you can format Date objects, compare dates, and create specific dates among other functionalities.\n    <\/p>\n<pre><code>import moment from 'moment';\n\nconst formattedDate = moment().format('YYYY-MM-DD HH:mm:ss');\nconsole.log(formattedDate); \/\/ Example: 2023-10-01 12:30:15\n<\/code><\/pre>\n<h3>Using Date-fns<\/h3>\n<p>\n<strong>date-fns<\/strong> is a much lighter library that allows you to import functions only when needed. It emphasizes functional programming style and supports various date-related tasks.\n    <\/p>\n<pre><code>import { format } from 'date-fns';\n\nconst formattedDate = format(new Date(), 'yyyy-MM-dd');\nconsole.log(formattedDate); \/\/ Example: 2023-10-01\n<\/code><\/pre>\n<h2>Managing Time Zones<\/h2>\n<p>\n        When dealing with dates and times, time zones are also an important factor to consider. The basic Date object in JavaScript operates according to the browser&#8217;s time zone, but using standardized time, such as UTC, is also important.\n    <\/p>\n<h3>Changing Time Zones<\/h3>\n<p>\n        Using moment.js, you can easily change time zones. With <strong>moment-timezone<\/strong>, you can convert dates to specific time zones.\n    <\/p>\n<pre><code>import moment from 'moment-timezone';\n\nconst newYorkTime = moment.tz('2023-10-01 12:00', 'America\/New_York');\nconsole.log(newYorkTime.format()); \/\/ Specific time in New York timezone\n<\/code><\/pre>\n<h2>Creating Custom Date Components<\/h2>\n<p>\n        In React, you can create customized components that display dates to improve the UI. For example, let&#8217;s create a date picker that allows users to easily select a date.\n    <\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst DatePicker = () =&gt; {\n    const [selectedDate, setSelectedDate] = useState(new Date());\n\n    const handleChange = (event) =&gt; {\n        setSelectedDate(new Date(event.target.value));\n    };\n\n    return (\n        <div>\n            <h2>Selected Date: {selectedDate.toLocaleDateString()}<\/h2>\n            <input onchange=\"{handleChange}\" type=\"date\"\/>\n        <\/div>\n    );\n};\n\nexport default DatePicker;<\/code><\/pre>\n<h2>Introduction to Recent Date Libraries<\/h2>\n<p>\n        Recently, various libraries have emerged to help manage complex date and time handling. <strong>Luxon<\/strong> offers all the functionalities we need and supports internationalization by default, making it easy to deal with different formats of dates and times.\n    <\/p>\n<pre><code>import { DateTime } from 'luxon';\n\nconst now = DateTime.now();\nconsole.log(now.toString()); \/\/ Output current date and time\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        The Date object in JavaScript is very useful for handling dates and times. However, when complex functionalities are required, or diverse formats are necessary, it is more efficient to use external libraries. In React, you can effectively use the Date object by leveraging state hooks and lifecycle hooks, making it easy to implement various date-related features.\n    <\/p>\n<p>\n        In this lecture, we covered the basic usage of the Date object along with various applications in React. Through this, developers will gain the ability to handle dates and times more effectively. We will explore additional cases for deeper understanding, and encourage further practice to master these functionalities.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dates and times are particularly important in modern web applications. Various functionalities such as appointments, schedules, timelines, and countdowns depend on dates and times. In this lecture, we will take a closer look at the Date object in JavaScript and discuss how to effectively use this object in a React environment. What is the Date &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32813\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Date Object and Dates&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[123],"tags":[],"class_list":["post-32813","post","type-post","status-publish","format-standard","hentry","category-react-basics-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32813\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Dates and times are particularly important in modern web applications. Various functionalities such as appointments, schedules, timelines, and countdowns depend on dates and times. In this lecture, we will take a closer look at the Date object in JavaScript and discuss how to effectively use this object in a React environment. What is the Date &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Date Object and Dates&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32813\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:11:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:34:49+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Date Object and Dates\",\"datePublished\":\"2024-11-01T09:11:44+00:00\",\"dateModified\":\"2024-11-01T12:34:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/\"},\"wordCount\":646,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32813\/\",\"name\":\"React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:11:44+00:00\",\"dateModified\":\"2024-11-01T12:34:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32813\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32813\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Date Object and Dates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32813\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Dates and times are particularly important in modern web applications. Various functionalities such as appointments, schedules, timelines, and countdowns depend on dates and times. In this lecture, we will take a closer look at the Date object in JavaScript and discuss how to effectively use this object in a React environment. What is the Date &hellip; \ub354 \ubcf4\uae30 \"React Course: Date Object and Dates\"","og_url":"https:\/\/atmokpo.com\/w\/32813\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:11:44+00:00","article_modified_time":"2024-11-01T12:34:49+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32813\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32813\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Date Object and Dates","datePublished":"2024-11-01T09:11:44+00:00","dateModified":"2024-11-01T12:34:49+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32813\/"},"wordCount":646,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32813\/","url":"https:\/\/atmokpo.com\/w\/32813\/","name":"React Course: Date Object and Dates - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:11:44+00:00","dateModified":"2024-11-01T12:34:49+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32813\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32813\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32813\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Date Object and Dates"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32813"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32813\/revisions"}],"predecessor-version":[{"id":38067,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32813\/revisions\/38067"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}