gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
javascript code , input utc dtate tile string , the I want to display date and time in…
- User
javascript code , input utc dtate tile string , the I want to display date and time in any timezone...
- Assistant
Here's an example code snippet that takes a UTC date/time string and converts it to a different timezone using the Moment.js library:
// Example UTC date/time string: "2022-05-10T14:30:00Z" const utcDateTimeStr = "2022-05-10T14:30:00Z"; // Get the Moment.js object for the UTC date/time const utcMoment = moment.utc(utcDateTimeStr); // Set the timezone you want to display the date/time in const timezone = "America/Los_Angeles"; // Example timezone: Pacific Time // Convert the Moment.js object to the desired timezone const timeZoneMoment = utcMoment.tz(timezone); // Format the timezone date/time as a string const formattedDateTimeStr = timeZoneMoment.format("YYYY-MM-DD HH:mm:ss"); // Display the formatted date/time string in your UI or console console.log(`Date/time in ${timezone}: ${formattedDateTimeStr}`);Note that you'll need to include Moment.js in your project for this code to work. You can add it to your HTML file with the following code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data.min.js"></script>