التواريخ باستخدام kotlinx-datetime
طريقة آمنة متعددة المنصات للتعامل مع الوقت والتواريخ
التواريخ باستخدام kotlinx-datetime درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Dates Need a Library
The core stdlib has no shared date type, so KMP uses kotlinx-datetime, an official multiplatform library for time.
Add the Dependency
You pull kotlinx-datetime into commonMain in your build file, and then every target can work with dates the same way.
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0")Instant Is a Moment
An Instant is a single point on the timeline, the same exact moment no matter which device reads it.
val now: Instant = Clock.System.now()Time Zones Matter
The same instant looks different per TimeZone. The library lets you convert moments into local wall-clock time safely.
val zone = TimeZone.currentSystemDefault()LocalDateTime for People
A LocalDateTime is the human-readable date and time, like a calendar entry, without any time-zone baggage attached.
val dt = now.toLocalDateTime(zone)Read the Parts
From a LocalDateTime you can read fields like year, month, day and hour, all in plain common Kotlin.
val y = dt.year
val h = dt.hourLocalDate for Calendars
When you only care about the day, use LocalDate. It models a date with no time component at all.
val today: LocalDate = dt.dateDo Math With Durations
You can add or subtract a Duration from an Instant to shift moments forward or backward in time.
val later = now.plus(1, DateTimeUnit.HOUR, zone)Parse and Format Text
kotlinx-datetime can parse ISO strings into instants and turn them back into text, ideal for API payloads.
val parsed = Instant.parse("2024-01-01T00:00:00Z")Identical On Every Target
Because the library is multiplatform, your date logic produces the same result on Android, iOS and desktop. 🙂
Keep Dates in Shared Code
Put all your time handling in commonMain with kotlinx-datetime, so the two apps never disagree about dates.
Quick Check
Which type marks one exact moment in time?
Recap
Use kotlinx-datetime for dates: Instant for moments, LocalDateTime for people, and shared code that agrees across platforms.
تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «التواريخ باستخدام kotlinx-datetime» مجاني؟
نعم — نص درس «التواريخ باستخدام kotlinx-datetime» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «التواريخ باستخدام kotlinx-datetime»؟
طريقة آمنة متعددة المنصات للتعامل مع الوقت والتواريخ تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «التواريخ باستخدام kotlinx-datetime»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- السلاسل والأرقام والمجموعات في كل مكان
- map وfilter وfold في التعليمات البرمجية المشتركة
- التواريخ باستخدام kotlinx-datetime
- ما ليس موجودًا في Common stdlib