lib.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. const [datePart] = dateString.split(' ');
  10. const [year, month, day] = datePart.split('-');
  11. return `${day}/${month}/${year}`;
  12. }
  13. export const convertToHKTime = (utcDateTimeString) => {
  14. const hkDate = new Date(utcDateTimeString).toLocaleString('en-HK', {
  15. timeZone: 'Asia/Hong_Kong',
  16. year: 'numeric',
  17. month: 'numeric',
  18. day: 'numeric'
  19. });
  20. const hkTime = new Date(utcDateTimeString).toLocaleString('en-HK', {
  21. timeZone: 'Asia/Hong_Kong',
  22. hour: '2-digit',
  23. minute: '2-digit',
  24. second: '2-digit',
  25. hour12: false
  26. });
  27. return { hkDate, hkTime };
  28. };
  29. // Example usage:
  30. // const utcDateString = "2024-08-22T09:00:00.000Z";
  31. // const hkTime = convertToHKTime(utcDateString);
  32. // console.log(hkTime); // Output will be in the format: YYYY/MM/DD/HH/MM/SS