---
title: DateTime.Time – ดึงเวลาจาก DateTime
url: https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-time/
type: function-explainer
program: Power Query
syntax: DateTime.Time(dateTime as any) as nullable time
date: 2025-12-03
updated: 2025-12-26
scores:
  popularity: 6
  difficulty: 2
  usefulness: 6
---

# DateTime.Time – ดึงเวลาจาก DateTime

> ฟังก์ชันที่ดึงเฉพาะส่วนเวลา (Time) ออกจากค่า DateTime เมื่อคุณต้องการแยกเวลาจากค่าวันที่และเวลารวมกั

## คำอธิบาย

ฟังก์ชันที่ดึงเฉพาะส่วนเวลา (Time) ออกจากค่า DateTime เมื่อคุณต้องการแยกเวลาจากค่าวันที่และเวลารวมกัน

## Syntax

```excel
DateTime.Time(dateTime as any) as nullable time
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| dateTime | Yes | datetime |  | ค่า DateTime ที่ต้องการดึงเวลา สามารถเป็น datetime value หรือ null |

## ตัวอย่าง

### 1. ดึงเวลาจาก DateTime พื้นฐาน

```excel
DateTime.Time(#datetime(2010, 12, 31, 11, 56, 02))
```

**ผลลัพธ์:** `#time(11, 56, 2)`

ดึงเฉพาะส่วนเวลา 11:56:02 ออกจาก DateTime ที่กำหนด ส่วนวันที่ 31 ธันวาคม 2010 ถูกตัดทิ้ง

### 2. ดึงเวลาจาก DateTime ที่มีมิลลิวินาที

```excel
DateTime.Time(#datetime(2025, 5, 20, 14, 30, 15))
```

**ผลลัพธ์:** `#time(14, 30, 15)`

ดึงเวลา 14:30:15 จากค่า DateTime ที่เป็นวันที่ 20 พฤษภาคม 2025 เลขชั่วโมง นาที วินาทีได้อย่างถูกต้อง

### 3. ใช้ DateTime.Time ในตาราง Power Query

```excel
= Table.AddColumn(Source, "Time Only", each DateTime.Time([DateTimeColumn]))
```

**ผลลัพธ์:** `คอลัมน์ใหม่ที่มีเฉพาะค่าเวลา ทั้งตาราง`

เพิ่มคอลัมน์ใหม่ชื่อ "Time Only" โดยใช้ DateTime.Time ดึงเวลาจากคอลัมน์ DateTimeColumn ทุกแถว แต่ละแถวจะได้ time value แยกออกมา

### 4. ดึงเวลาเพื่อจำแนกช่วงเวลา

```excel
= let TimeValue = DateTime.Time(#datetime(2025, 5, 20, 14, 30, 15)) in if TimeValue >= #time(9, 0, 0) and TimeValue < #time(17, 0, 0) then "Business Hours" else "After Hours"
```

**ผลลัพธ์:** `"Business Hours"`

ดึงเวลาแล้วเปรียบเทียบว่าอยู่ในช่วงเวลาทำงาน (9:00-17:00) หรือไม่ ผลลัพธ์คือ "Business Hours" เพราะเวลา 14:30 อยู่ในช่วงนั้น

## หมายเหตุเพิ่มเติม

- ผมแนะนำให้ใช้ DateTime.Time ร่วมกับ Table.AddColumn เพื่อเพิ่มคอลัมน์เวลาใหม่ลงในตาราง ทำให้ง่ายต่อการวิเคราะห์เวลา

- ส่วนตัวผม ชอบใช้ DateTime.Time ร่วมกับการเปรียบเทียบเวลา (#time) เพื่อจำแนกช่วงเวลา เช่นแยกเวลากลางวันกับกลางคืน

- ใช้ try-catch หรือ if statement ในการรับมือกับค่า null เพื่อไม่ให้ query ขาดหาย

## คำถามที่พบบ่อย

**Q: DateTime.Time กับ DateTime.Date ต่างกันอย่างไร?**

ผม จะอธิบายความแตกต่างนะ DateTime.Date ดึงแค่วันที่ (date) ทิ้งเวลาไป แต่ DateTime.Time ดึงแค่เวลา (time) ทิ้งวันที่ไป ใช้ตามความต้องการของข้อมูลที่ต้องการวิเคราะห์

**Q: ถ้า input เป็น null จะเป็นไง?**

ผมเคยพบปัญหานี้ DateTime.Time จะคืนค่า null กลับมา ถ้าค่า input เป็น null ต้องใช้ if statement เพื่อตรวจสอบก่อน เช่น if input = null then null else DateTime.Time(input)

**Q: ใช้ DateTime.Time ได้กับ Power BI ไหม?**

ได้ครับ ผมใช้ในทั้ง Power BI Desktop และ Excel Power Query ทำหน้าที่เดียวกัน คือดึงเวลาจาก DateTime

## ฟังก์ชันที่เกี่ยวข้อง

- [DateTime.Date – ดึงส่วนวันที่จาก DateTime](https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-date/)
- [DateTime.From – แปลงค่าเป็น DateTime](https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-from/)
- [DateTime.LocalNow – วันและเวลาปัจจุบันของเครื่อง](https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-localnow/)
- [Time.From – แปลงค่าเป็นเวลา](https://www.thepexcel.com/functions/power-query/time-functions/time-from/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn - DateTime.Time](https://learn.microsoft.com/en-us/powerquery-m/datetime-time) _(official)_
- [PowerQuery.how - DateTime.Time Guide](https://powerquery.how/datetime-time/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-time/](https://www.thepexcel.com/functions/power-query/datetime-functions/datetime-time/)_
