0

I want to change the Time Zone from GMT+0000 to GMT+7

I coded it in Replit

my code:

client.on('messageCreate', async (message) => {

    var readmessagefile = fs.readFileSync('log.txt', 'utf-8');
    var writemessagefile = fs.writeFileSync('log.txt', 'Time - ' + 
    message.createdAt + ' User - ' + message.member.user.username + ': ' + message.content + `\n` + readmessagefile);
)};

result:

Time - Wed Jul 12 2023 00:24:14 GMT+0000 (Coordinated Universal Time) User - haikalmabrur: k.help
Time - Wed Jul 12 2023 00:24:10 GMT+0000 (Coordinated Universal Time) User - haikalmabrur: a

I want the result like this:

Time - Wed Jul 12 2023 07:24:14 GMT+7 User - haikalmabrur: k.help
Time - Wed Jul 12 2023 07:24:10 GMT+7 User - haikalmabrur: a

1 Answers1

1

createdAt is a javascript date object. You can use toLocaleString()

options = {
    timeZone: "America/Los_Angeles",
    weekday: "short" ,
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
    timeZoneName: "shortOffset",
}
var d = message.createdAt
var str = d.toLocaleString('en-US', options)
// 'Tue, Jul 11, 2023, 6:26:15 PM GMT-7'

If you don't want the commas, you can get each part of the time string individually and concatenate them:

var d = message.createdAt
var str = d.toLocaleString('en-US', { timeZone: "America/Los_Angeles", weekday: "short" }) + ' ' +
        + d.toLocaleString('en-US', { timeZone: "America/Los_Angeles", year: "numeric" })

Also see How to initialize a JavaScript Date to a particular time zone for libraries and explanation of the Date object.

EJam
  • 164
  • 5