IT박스

날짜-시간 C #에서 시간 만 가져 오는 방법

itboxs 2020. 7. 13. 21:43
반응형

날짜-시간 C #에서 시간 만 가져 오는 방법


값이 2009 년 6 월 22 일 오전 10:00:00이라고 가정합니다. 이 날짜 시간에서 오전 10 시만받는 방법은 무엇입니까?


이에 대한 많은 옵션이 있습니다.

DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");

dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock

유용한 링크 : DateTime.ToString () 패턴


에서 DateTime사용할 수 .TimeOfDay있지만- TimeSpan하루의 시간 (10 시간)을 나타냅니다.


C #에는 DateTime 형식 만 있으며 날짜 및 시간 부분으로 구성됩니다. DateTime의 Date 부분에 신경 쓰지 않으면 다음과 같이 기본값으로 설정하십시오.

DateTime myTime = default(DateTime).Add(myDateTime.TimeOfDay)

이렇게하면 Microsoft가 기준 날짜를 1/1/0001 이외의 다른 것으로 변경하기로 결정하더라도 모든 .NET 버전에서 일관성을 유지할 수 있습니다.


DateTime.ToShortTimeString () 메서드 를 살펴볼 수 있습니다 .

또한 DateTime 개체에는 원하는 방식으로 날짜 또는 시간의 형식을 지정하는 데 도움이되는 다른 많은 메서드와 속성 이 있습니다. 설명서를 살펴보십시오.


이 시도:

TimeSpan TodayTime = DateTime.Now.TimeOfDay;

그렇게하는 방법에는 여러 가지가 있습니다. DateTime.Now.ToLongTimeString()문자열 형식으로 시간 만 반환 하는 which 를 사용할 수 있습니다 .


다음 코드를 사용할 수 있습니다.

DateTime dt = new DateTime(2009, 6, 22, 10, 0, 0);  //Date 6/22/2009 10:00:00 AM
string time = dt.ToString("hh:mm tt");              //Output: 10:00 AM
time = dt.ToString("HH:mm tt");                     //Output: 10:00 AM
dt = new DateTime(2009, 6, 22, 22, 0, 0);           //Date 6/22/2009 10:00:00 PM
time = dt.ToString("hh:mm tt");                     //Output: 10:00 PM
time = dt.ToString("HH:mm tt");                     //Output: 22:00 PM

이것을 사용할 수 있습니다

lblTime.Text = DateTime.Now.TimeOfDay.ToString(); 

밀리 초 값으로 실시간이며 시간으로 만 설정됩니다.


간단히 쓸 수 있습니다

string time = dateTimeObect.ToString("HH:mm");

DateTime now = DateTime.Now;

now.ToLongDateString();    // Wednesday, January 2, 2019
now.ToLongTimeString();    // 2:33:59 PM
now.ToShortDateString();   // 1/2/2019
now.ToShortTimeString();   // 2:16 PM
now.ToString();            // 1/2/2019 2:33:59 PM

이것은 나를 위해 작동합니다. 날짜 부분 만 가져 오기 위해 DateTime.Date와 함께 작업해야 할 때 발견했습니다.

var wholeDate = DateTime.Parse("6/22/2009 10:00:00 AM");
var time = wholeDate - wholeDate.Date;

ToString("T")장시간 또는 ToString("t")단시간에 사용할 수 있습니다 .


gridview를 사용하는 경우 DataFormatString="{0:t}"들어 시간 만 표시 할 수 있습니다 .

    By bind the value:-
<asp:Label ID="lblreg" runat="server" Text='<%#Eval("Registration_Time ", "{0:t}") %>'></asp:Label>

By bound filed:-
<asp:BoundField DataField=" Registration_Time" HeaderText="Brithday" SortExpression=" Registration Time " DataFormatString="{0:t}"/>

If you're looking to compare times, and not the dates, you could just have a standard comparison date, or match to the date you're using, as in...

DateTime time = DateTime.Parse("6/22/2009 10:00AM");
DateTime compare = DateTime.Parse(time.ToShortDateString() + " 2:00PM");
bool greater = (time > compare);

There may be better ways to to this, but keeps your dates matching.


You need to account for DateTime Kind too.

public static DateTime GetTime(this DateTime d)
{
    return new DateTime(d.TimeOfDay.Ticks, d.Kind);
}

In my case this work good

MyDateTime.ToLongTimeString()

Easy

DateTime dt = DateTime.Now;

string Timeonly = dt.ToLongTimeString();

Greetings

참고URL : https://stackoverflow.com/questions/1026841/how-to-get-only-time-from-date-time-c-sharp

반응형