lib.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // Example usage:
  36. // const utcDateString = "2024-08-22T09:00:00.000Z";
  37. // const hkTime = convertToHKTime(utcDateString);
  38. // console.log(hkTime); // Output will be in the format: YYYY/MM/DD/HH/MM/SS