Are TypeScript Date Formatting options comparable to those found in .NET?

When working with C# .NET, adjusting the Format of a date is simple:

DateTime date = new DateTime();
string date1 = date.ToString("dd MMM yy");
string date2 = date.ToString("yyyy/MM");
string date3 = date.ToString("MMMM d, yyyy");

However, I'm curious to know how I can accomplish similar date formatting in TypeScript?

Answer №1

While browsing the web, I stumbled upon this JavaScript file on GitHub created by marlun78.

I decided to contribute to the TypeScript community by sharing my rewritten version of the code here. Take a look:

export interface IFormatDateSettings {
days?: string[];
... (Code continues with various formatting settings and patterns)
}
...
// Format Date Service Class containing various date formatting methods

Feel free to utilize it as follows:

let date: Date = new Date();
let formatDateService = new FormatDateService();
let formattedDate1: string = formatDateService.formatDate(date, 'dd MM yy');
let formattedDate2: string = formatDateService.formatDate(date, 'yyyy/MM'); 
let formattedDate3: string = formatDateService.formatDate(date, 'MMMM d, yyyy');    

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

Tips for ensuring consistent resolution of absolute module paths in Visual Studio using TypeScript and SystemJS

In my journey to master module management in TypeScript 1.8.4 with SystemJS, I rely on Visual Studio 2013 as my trusty IDE. Things flow smoothly when I stick to relative paths for importing modules: Import { fooThing } from “../myThings/things/foo”; ...

How can I invoke TypeScript methods within a jQuery event handler in the ngAfterViewInit lifecycle hook?

I am currently utilizing Angular 4. I need to invoke methods from a typescript file within the ngAfterViewInit method. declare var $; @Component({ selector: 'app-details', templateUrl: './details.component.html', styleUrls: [&apo ...

Upon receiving data from the Api, the data cannot be assigned to the appropriate datatype within the Angular Object

I am encountering an issue with the normal input fields on my page: https://i.stack.imgur.com/qigTr.png Whenever I click on the "+" button, it triggers an action which in turn calls a service class with simple JSON data. My intention is to set selectionC ...

The error message "The property 'handleLoginDisplay' is not found in the type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'" is displayed

When I try to compile this TypeScript code, it throws an error. Can anyone help me understand why? import { connect } from 'react-redux'; import React from "react"; import { Link, Redirect } from "react-router-dom"; class HeaderComponent extend ...

Support for dark mode in Svelte with Typescript and TailwindCSS is now available

I'm currently working on a Svelte3 project and I'm struggling to enable DarkMode support with TailwindCSS. According to the documentation, it should be working locally? The project is pretty standard at the moment, with Tailwind, Typescript, and ...

"Encountering issues with the functionality of two Angular5 routers

main.component.html [...] <a routerLink="/company-list">Open</a> [...] <main> <router-outlet name="content"><router-outlet> </main> [...] app.compoment.html <router-outlet><router-outlet> app.routing.modu ...

The drop-down list data binding function is not only refreshing the list but also incorporating any existing data

So I have a form that inserts values into a DropDownList. However, when I call DropDownList.DataBind() ; inside the save function, it keeps appending the new field I just typed to the existing options in the DropDownList. Any suggestions on how to pre ...

The overlay lingers on the page even after the modal successfully redirects to the search

My goal is to implement a modal that prompts users to input their search term and find recipes with a specific ingredient. I have successfully achieved this by using simple redirection. However, upon redirecting to the search results, the overlay remains o ...

Angular 7: An unexpected error occurred when trying to inject TripsMenu into MenuWidgetComponent in AppModule

Encountering an issue in angular 7, where I am trying to inject my MenuWidgetComponent in the home component. I have imported it in the widget component and exported it via index.ts. However, the following error persists: I searched online but couldn&apos ...

Do folders in the Windows operating system possess unique identifiers known as IDs or GUIDs?

I have a specific task where I am selecting a folder and copying it. I want to avoid recopying the folder if it gets renamed. Is there an identifier for each folder in Windows that remains constant until the folder is deleted? Preferably looking for a sol ...

ReactNative: When attempting to navigate, a TypeError occurred - this.props.navigation.navigate is not a function

It's confusing to see how this issue is occurring, even though all props have been properly typed. The goal is to pass the navigator to the bottom bar component in order to navigate onPress. Initially, I define the props interface: export interface B ...

Troubleshoot: Angular 2 Carousel not Showing up on Page

Having trouble understanding why my carousel component is present but none of the CSS or images are showing up. This is my app.component.ts file: import { Component } from '@angular/core'; // Import our Carousel Component import {CSSCarouselCo ...

Preventing duplicate namespace declarations in TypeScript

Let's say I have a variety of objects with the following data structure: { namespace: 'first'|'second'|'third' } Now, I need to include another object with the same data structure, but its namespace cannot be assigned ...

The absence of a reference to the AsyncCtpLibrary.dll is causing an issue

After installing the API for utilizing the new feature of C# 5, I encountered some unexpected issues. I was able to use the async keyword but Visual Studio 2010 kept showing me an error message saying "Can not find Task-Related types. Are you missing a re ...

Guide for parsing JSON with dynamic keys in C#

I've been struggling with a similar issue for the past 12 hours and couldn't find a solution. If anyone can offer advice on how to resolve my problem, I would greatly appreciate it. Here is an example of my JSON response: { "status": ...

Vue.js 3 with TypeScript is throwing an error: "Module 'xxxxxx' cannot be located, or its corresponding type declarations are missing."

I developed a pagination plugin using Vue JS 2, but encountered an error when trying to integrate it into a project that uses Vue 3 with TypeScript. The error message displayed is 'Cannot find module 'l-pagination' or its corresponding type ...

Storing a Custom Class Object in Sql Server 2008 with C#: Best Practices

Question: Saving byte[] into a SQL Server database using C# In my project, I have created a class called LeaveDetails.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace LeaveMgmt_14dec { [Serializab ...

"The jQuery AJAX function is displaying an error message indicating that the resource was

Does anyone have experience using jQuery AJAX to send data from the client side to the server? I thought it would be straightforward, but I keep getting a "not found" error. Here's the AJAX code I'm using: <script type="text/javascript"> ...

What is a more efficient way to optimize the repeating 'for-of' loop?

I am faced with the following code challenge: runA() { const request = []; for (const item of this.items ) { request.push(this.getRequestData(item.prop1)); // want to generalize that } return forkJoin(...request).pipe(x => x); ...

Utilizing a service within NestJS

I'm currently in the process of updating some older code and I have created a service that I want to inject into the constructor of a class. There are two key points to consider about this particular class. The first point is that it is instantiated b ...