Tips for using Luxon to format timestamps

Recently, I decided to make the switch from using moment.js to Luxon for handling dates and times. With moment.js, I was able to convert a Date() object to timestamp format like this:

....moment()
      .subtract(2, 'hours')
      .utc()
      .format('YYYY-MM-DD HH:mm:ss [GMT]');

This resulted in a timestamp like: 2022-12-01 17:40:16 GMT

However, after switching to Luxon and reviewing the documentation, I couldn't find a straightforward way to achieve the same result. I tried the following approach:

const tryThis = date.minus({ hours: 2 }).toISO();
const getGMTIntheDate = DateTime.fromISO(tryThis);
const finalResult = getGMTIntheDate.toJSDate().toString();

This code produced a timestamp that looks like:

Thu Dec 01 2022 09:49:44 GMT-0800 (Pacific Standard Time)

I'm wondering if Luxon offers any custom formatting options that I may have overlooked in the documentation?

Answer №1

This is exactly the type of solution I had been seeking

date.convertToUTC().format('yyyy-MM-dd HH:mm:ss ZZZZ')

Answer №2

It seems like you may be interested in the datetimetoformat function. For more information on formatting, you can check out this link.

To get the current date and time formatted as 'yyyy-MM-dd HH:mm:ss [GMT]', use DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss [GMT]').

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to utilize Stats.js with @angular/cli version 1.4.4

Attempting to utilize @types/stats with @angular/cli following the guidance at https://github.com/angular/angular-cli/wiki/stories-third-party-lib. However, encountering a tslint error when trying to import * as STATS from 'stats.js'. [ts] Modul ...

Filtering an array of objects within another array in Javascript based on specific criteria

Here is an array example : exampleArray = [ { category: 'Fruits', items: [ { id: 1, name: 'Apple', selected: true }, { id: 2, name: 'Banana', selected: false }, { id: 3, name: 'Orange', selected: true } ] } ...

When using the Composition API in Vue 3, the "Exclude" TypeScript utility type may result in a prop validation error

Currently, I am utilizing Vue 3 alongside the Composition API and TypeScript, all updated to their latest stable versions. If we take a look at the types below: export interface Person { name: string; } export type Status = Person | 'UNLOADED&ap ...

What is the most graceful method to define a class attribute just once?

Is there a clean and efficient way to set a value only once within a TypeScript class? In other words, is there a way to make a value read-only after it has been assigned? For instance: class FooExample { public fixedValue: string; public setFixe ...

Unveiling Angular library dependencies

Is there a way to conceal internal dependencies when developing an angular library? For example, during the development of my library, I added this dependency: yarn add moment-es6 However, I want to keep this as only an internal dependency and not impos ...

Is there a way to delete values from a JavaScript object and include an index column?

I currently have this incoming dataset: [] 0:{"time": 1525355921817, "sym": "AAPL", "price": 169.16, "size": 98, "stop": false, …} 1:{"time": 1525355923216, "sym": "AAPL", "price": 170.15, "size": 6, "stop": false, …} 2:{"time": 1525355923216, "sym": " ...

Issues arise when a Koa Typescript application stalls upon attempting to make any database operations within

My current setup involves a basic koa REST service written in typescript. It consists of a simple GET request that aims to retrieve all users from a firebase database located at db reference /user. Upon receiving the request, the application successfully ...

I wonder what the response would be to this particular inquiry

I recently had an angular interview and encountered this question. The interviewer inquired about the meaning of the following code snippet in Angular: code; <app-main [type]="text"></app-main> ...

Applying a generic function to every property of an object in TypeScript to deduce the return value

I'm facing an issue with TypeScript where I need to solve a problem: type State = { alpha: string, beta: number } const selectors = { alpha: (s: State) => s.alpha, beta: (s: State) => s.beta } function createModifiedSelector< ...

I am trying to move to a different page, but for some reason the router.navigate function is not functioning within the subscribe

//I am attempting to redirect to another page once the subscribe method is executed, however I am encountering issues with the router.navigate function within the subscribe method. //In an effort to address this issue, I have attempted to store the data r ...

Angular error: Attempting to reduce an empty array without an initial value

I am encountering an issue with my array being filtered and reduced. getPageComponents(title: string) { this.pageComponents = []; const pageBlock = this.pageComponents.filter((val) => { if (val.page_title === title) { retur ...

Angular page not reflecting show/hide changes from checkbox

When the user clicks on the checkbox, I need to hide certain contents. Below is the code snippet: <input id="IsBlock" class="e-field e-input" type="checkbox" name="IsBlock" style="width: 100%" #check> To hide content based on the checkbo ...

Angular component showcasing the usage of a nested input-group

I have developed an input component and a datepicker component in Angular. The input component generates the appropriate input tag based on the type parameter, whether it's text, number, etc. Meanwhile, the date picker is another component that contai ...

Passing parameters by reference in TypeScript

It is possible to pass parameters by reference in C#. For example: private void Add(ref Node node) { if (node == null) { node = new Node(); } } Add(ref this.Root); After executing Add(ref this.Root), th ...

The default value is not displayed in the Angular dropdown menu

When using regular html select menus, if you create an option element with selected and disabled attributes and provide text for that option, the text will be displayed by default in the select menu. Below is a basic example of HTML code: <select name= ...

I am hoping for the bootstrap collapse feature to automatically close one section when another is expanded

I tested this code and it worked for me, but when I try to use it in Angular, I encounter an error in jQuery stating that "collapse tag is not found in jQuery". Can anyone assist me in resolving this issue? $('.button-click').click( function(e ...

Uncover the SES communication from S3 using KMS and NodeJS for decryption

I'm struggling to decrypt the messages I receive from my S3 bucket, which are encrypted with a KMS key. I am using Node and Typescript. I have attempted various methods but have not been successful. I have looked into the following links: https://git ...

Building a TypeScript function using a dictionary with function names and argument types

In possession of a dictionary { "function_name":"myFunc", "arguments":["a1","a2"] } A desire to create a function where the name matches that in the dictionary above (i.e myFunc ) with respective arg ...

Exploring object types in React with Typescript

When working with React/Typescript, what type should be used for an object? For instance: const [value, setValue] = React.useState<any>({}); If you need to specifically define it as an object, what type would you use instead of <any>? ...

Ways to enable Urql (typescript) to accept Vue reactive variables for queries generated using graphql-codegen

I'm currently developing a Vue project that involves using urql and graphql-codegen. When utilizing useQuery() in urql, it allows for the use of Vue reactive variables to make the query reactive and update accordingly when the variables change. The ...