lib.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export function formatDate(dateString) {
  2. const date = new Date(dateString);
  3. const day = date.getUTCDate().toString().padStart(2, '0');
  4. const month = (date.getUTCMonth() + 1).toString().padStart(2, '0'); // getUTCMonth() returns 0-11
  5. const year = date.getUTCFullYear();
  6. return `${day}/${month}/${year}`;
  7. }
  8. export function formatCouponDate(dateString) {
  9. if (dateString === null) {
  10. return '永久';
  11. }
  12. // Use the Date object to properly parse the ISO string
  13. const date = new Date(dateString);
  14. const day = date.getUTCDate().toString().padStart(2, '0');
  15. const month = (date.getUTCMonth() + 1).toString().padStart(2, '0');
  16. const year = date.getUTCFullYear();
  17. return `${day}/${month}/${year}`;
  18. }
  19. export const convertToHKTime = (utcDateTimeString) => {
  20. const hkDate = new Date(utcDateTimeString).toLocaleString('en-HK', {
  21. timeZone: 'Asia/Hong_Kong',
  22. year: 'numeric',
  23. month: 'numeric',
  24. day: 'numeric'
  25. });
  26. const hkTime = new Date(utcDateTimeString).toLocaleString('en-HK', {
  27. timeZone: 'Asia/Hong_Kong',
  28. hour: '2-digit',
  29. minute: '2-digit',
  30. second: '2-digit',
  31. hour12: false
  32. });
  33. return { hkDate, hkTime };
  34. };
  35. export function formatToChineseDateTime(dateString: string): string {
  36. const date = new Date(dateString);
  37. // Add 8 hours for HK timezone
  38. date.setHours(date.getHours());
  39. const year = date.getFullYear();
  40. const month = date.getMonth() + 1;
  41. const day = date.getDate();
  42. const hours = date.getHours();
  43. const minutes = date.getMinutes();
  44. // Pad minutes with leading zero if needed
  45. const paddedMinutes = minutes.toString().padStart(2, '0');
  46. return `${month}月${day}日 ${hours}:${paddedMinutes}`;
  47. }
  48. // Example usage:
  49. // const utcDateString = "2024-08-22T09:00:00.000Z";
  50. // const hkTime = convertToHKTime(utcDateString);
  51. // console.log(hkTime); // Output will be in the format: YYYY/MM/DD/HH/MM/SS