C\#현재 총 밀리초 수의 인 스 턴 스 설명 가 져 오기

2776 단어 C#밀리 초
.Net 에서 DateTime.Ticks 는 long 형의 시간 정 수 를 얻 었 다.구체 적 으로 는 0001 년 1 월 1 일 자정 12:00:00 이후 거 친 시간 이 100 나 초 라 는 숫자 를 나타 낸다.초 는 Ticks/10000000,밀리초 Ticks/10000 으로 전환 합 니 다.
1970 년 1 월 1 일부 터 현재 시간 까지 의 밀리초 수 를 가 져 오 려 면 코드 는 다음 과 같 습 니 다.

//    Ticks
long currentTicks= DateTime .Now.Ticks;
DateTime dtFrom = new DateTime (1970, 1, 1, 0, 0, 0, 0);
long currentMillis = (currentTicks - dtFrom.Ticks) / 10000;
자바
환산 단위:
1 초=1000 밀리초
1 밀리초=1000 미묘
1 초
보충:C\#타임 스탬프 byte[]를 datetime 로 바 꾸 는 몇 가지 방법
추천 방법:

DateTime now = DateTime.Now;
byte[] bts = BitConverter.GetBytes(now.ToBinary());
DateTime rt = DateTime.FromBinary(BitConverter.ToInt64(bts, 0)); 
byte 2 개 를 사 용 했 습 니 다.날짜 범 위 는 2000-01-01~2127-12-31 입 니 다.다음은 전환 방법 입 니 다.

 // Date -> byte[2] 
 public static byte[] DateToByte(DateTime date) 
 { 
  int year = date.Year - 2000; 
  if (year < 0 || year > 127) 
  return new byte[4]; 
  int month = date.Month; 
  int day = date.Day; 
  int date10 = year * 512 + month * 32 + day; 
  return BitConverter.GetBytes((ushort)date10); 
 } 
 // byte[2] -> Date 
 public static DateTime ByteToDate(byte[] b) 
 { 
  int date10 = (int)BitConverter.ToUInt16(b, 0); 
  int year = date10 / 512 + 2000; 
  int month = date10 % 512 / 32; 
  int day = date10 % 512 % 32; 
  return new DateTime(year, month, day); 
 } 
호출 예:

byte[] write = DateToByte(DateTime.Now.Date); 
MessageBox.Show(ByteToDate(write).ToString("yyyy-MM-dd"));

/// <summary> 2. ///  BYTE     DATETIME   3. /// </summary> 4. /// <param name="bytes"></param> 5. /// <returns></returns> 6. private DateTime BytesToDateTime(byte[] bytes)
 {
  if (bytes != null && bytes.Length >= 5)
  {
  int year = 2000 + Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[0] }, 0));
  int month = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[1] }, 0));
  int date = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[2] }, 0));
  int hour = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[3] }, 0));
  int minute = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[4] }, 0));
  DateTime dt = new DateTime(year, month, date, hour, minute, 0);
  return dt;
  }
  else19.  {
  return new DateTime();
  }
 }
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기