What is the method for removing the utc value from a luxon datetime?

I am facing an issue with a Luxon DateTime format that has a UTC offset value of -08:00. The DateTime looks like this:

this.eventParams.date.toString()
=> '2023-02-23T00:00:00.000-08:00'

How can I reset this offset to 0 in Luxon?

The problem arises when passing this value to my C# controller, as it converts the utc offset to a TimeOfDay:

// .Net Controller
eventParams.Date
=> {02/23/2023 08:00:00}

eventParams.Date is defined as:

public DateTime? Date { get; set; }

Answer №1

To set a new DateTime instance with the time zone configured to UTC, you can utilize the method DateTime.toUTC(offset, opts)

For example:

<script type="module>

import * as luxon from "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f29e878a9d9cb2c1dcc0dcc3">[email protected]</a>/build/es6/luxon.js";

const dtLocal = luxon.DateTime.now();
const dtUtc = dtLocal.toUTC();

console.log("local:", dtLocal.toString());
console.log("utc:", dtUtc.toString());

</script>

Answer №2

In essence, what you're really asking is how to obtain a specific date in UTC rather than local time. The issue lies in creating a "day at midnight" as a local datetime and then transmitting it to a UTC server, which results in an 8-hour time difference. To resolve this discrepancy:

const myDate = DateTime.local(2023, 2, 23);

You should use the following instead:

const myDate = DateTime.utc(2023, 2, 23);

It's important to note that this will represent a local time of 14:00 the day before, but that aligns with your objective.

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

Best practices for using BehaviorSubject subscribe in Ionic Angular

I am currently implementing behaviorSubject in my Ionic Angular app to trigger data refresh. The subscription is placed within the ionViewDidEnter method of the first tab as shown below: async ionViewDidEnter(){ this.sharedSvc.getDataUpdateValue().subs ...

Identifying the Click Event Within an ngx Bootstrap Modal

I recently set up an ngx bootstrap modal using the instructions provided in this helpful guide - . However, I'm facing a challenge in detecting click events within the modal body once it's open. Below is the code snippet from my app component. D ...

Change the default icon in mat-stepper angular material

I'm attempting to customize the default edit icon in Angular Material's stepper component, but my attempts have been unsuccessful. Here is what I have tried: <mat-horizontal-stepper [linear]="isLinear" #stepper> <mat-step> ...

Observable not defined in facade pattern with RxJS

(After taking Gunnar's advice, I'm updating my question) @Gunnar.B Tool for integrating with API @Injectable({ providedIn: 'root' }) export class ConsolidatedAPI { constructor(private http: HttpClient) { } getInvestments(search?: ...

Guide to obtaining ngPrime autocomplete text when the button is clicked within Angular 6

I am currently utilizing the ngPrime autocomplete feature to retrieve data from a service. My goal is to capture the text entered in the autocomplete field whenever a button is clicked. I attempted to access the value using getElementById.value within th ...

angular2-seed-advanced encountered an error: RangeError - The maximum call stack size has been exceeded

Attempting to launch the angular-seed-advanced project without modifications on various platforms has been successful for web and desktop (Linux/Windows). However, when trying to run it on Android (emulator and actual device), the following error occurred: ...

Using TypeScript to specify precise types for ReactWrapper within the Enzyme library

During my testing process of custom components with jest and enzyme using typescript, I have been creating tests like this: const wrapper = mount(<MyCustomComponent/>); expect(wrapper).toMatchSnapshot(); As mount returns a type of ReactWrapper, I h ...

Managing Multiple File Inputs in React Using useRef Array Results in an Undefined Value

In the process of developing a React application, I encountered an issue where multiple file input elements were dynamically rendered using the Swiper component and Dropzone. However, upon attempting to retrieve all the selected files from these inputs dur ...

Troubleshooting problems updating FormArrays values with server data in Angular

Within my Angular form, users are able to input data and select multiple books using checkboxes before sending the information to the server. If a user wishes to edit the form at a later time, it should display with the previously submitted data pre-filled ...

"Unexpected Type Inference Issue: A variable initially defined as a string inexplicably transforms into 'undefined'

Currently, I am incorporating the await-to-js library for handling errors (specifically utilizing the to method from the library). In an intriguing scenario, the variable type shifts to string | undefined within a for..of loop, whereas outside of the loop ...

Issues with the functionality of Angular Material prebuilt themes are causing inconsistencies

After spending a considerable amount of time trying to understand Angular Material Themes, I decided to start by importing a prebuilt theme. However, I encountered some issues along the way. The theme doesn't seem to apply to all the tags as expected. ...

Encountering challenges when trying to connect an Angular application with Single Sign-On on the Teams Desktop client

We have created an angular application that is hosted on AKS with SSO enabled through Microsoft Entra App. For the login functionality, we are following the same process as outlined in this link. We were able to successfully integrate the application into ...

Submit a pdf file created with html2pdf to an S3 bucket using form data

Currently, I have the following script: exportPDF(id) { const options = { filename: 'INV' + id + '.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2, dpi: 300, letterRendering: true, useC ...

Tips for limiting users to inputting only alphanumeric characters and excluding special characters in an input field using Angular 8

How can I prevent users from inputting special characters in an input field and only allow alphanumeric values? The code that I have implemented so far does not seem to be working as intended. When a user enters a special character, it still shows up in th ...

Using @emotion/styled alongside esbuild has caused an issue where importing styled11 as default.div is not functioning as expected

Working on building a website using esbuild, react, and emotion/MUI has been smooth sailing so far. However, I've hit a roadblock with getting the styled component from @emotion/styled to function properly. uncaught TypeError: import_styled11.default ...

Transform a string with delimiter into a JSON object containing key-value pairs extracted from the string

Looking to transform a string into an object in typescript for NodeJS API var string = "1234|Tom|NYC|Student|Active" The goal is to map the string to: { "Id": 1234, "Name": "Tom", "City": "NYC ...

leveraging React TypeScript to retrieve XRM WebAPI data

Could someone please help me with the issue in this code? When I try to run it in my vscode, I get an error that says 'Xrm not defined'. Here is the code snippet: async function GetEnvironmentVariableValue(name: string): Promise<string | null& ...

Mapping properties between objects in Typescript: transferring data from one object to another

Here are two different types and an object: type TypeX = { x: number; y: number; z: number; }; type TypeY = { u: number; v: number; w: number; }; initialObject: { [key: string]: TypeX }; The goal is to transfer the properties from an object of ...

Insert data into Typeorm even if it already exists

Currently, I am employing a node.js backend in conjunction with nest.js and typeorm for my database operations. My goal is to retrieve a JSON containing a list of objects that I intend to store in a mySQL database. This database undergoes daily updates, bu ...

React props conditional type for prop types

Can you help me come up with a type for my component? It has two fields: title and description. Both can be undefined, but the description cannot exist without a title. Here are some examples to clarify: <Component title='str' description=&apo ...