Steps for transforming 112889 (in mmddyy format) into 11/28/89 or 11/28/1989

Is there a way to convert the unformatted date 112889 (mmddyy) into a specific format like 11/28/89?

console.log(new Date('112889'))
// The output I'm getting is: Sat Jan 01 112889 00:00:00 GMT+0800

I've searched extensively on Google for answers but haven't found any concrete solutions.

Reference searches:

  1. Convert number into date using javascript

I also considered using momentjs for formatting, but couldn't find relevant documentation or perhaps I missed it.

Answer №1

By making use of the momentjs library, you have the ability to specify the format in which your input string will be provided, such as MMDDYY.

var date = new moment("112889", "MMDDYY");
document.getElementById('dated').innerText = date.format('MM/DD/YYYY');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<div id="dated"></div>

Answer №2

Take a moment to explore the features of the moment.js library. Give this code snippet a try:

    var date = moment('112889',"MMDDYY").format('MM/DD/YY');
    console.log(data);

Answer №3

In the case where you prefer not to use a library and are sure that your input will always be in the format mmddyy, you can implement the following solution:

var str = '112889';
var arr = str.match(/.{1,2}/g); // Divides string into 2 character sections
var d = new Date(arr[2],arr[0],arr[1]);
console.log(d);

However, this method has its limitations as if you provide a string like 112804, JavaScript (or any other language) will default to interpreting it as 1904 instead of 2004. To account for this ambiguity, you should make an explicit specification :

var str = '112896';
var arr = str.match(/.{1,2}/g); //Divides string into 2 character section
var d = new Date(arr[2], arr[0], arr[1]); 
var presentYear = Number((new Date()).getFullYear());
var dateYear = Number(d.getFullYear());

if (presentYear >= dateYear + 100) { 
  d = new Date('20' + arr[2], arr[0], arr[1]);
}
console.log(d);

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

Approach to Using Bootstrap 4 with Intl-Tel-Input

I've been attempting to integrate intl-tel-input with bootstrap 4, but I'm facing an issue where the placeholder for the input is not showing up. I've tried various solutions found online, but none of them seem to work. Here's my HTML ...

Changing the value of an object in Angular can be achieved by utilizing the two

I have a service with the following methods: getLastStatus(id): Observable<string> { let url_detail = this.apiurl + `/${id}`; return this.http.get<any>(url_detail, this.httpOptions).pipe( map(data => { ...

Troubleshooting: Dealing with Stack Overflow Error when using setInterval in Vue Programming

I am facing a stack overflow error while creating a timer using Vue, and I'm struggling to understand the root cause. Can someone provide insights on the following code: Here is my template structure: <span class="coundown-number"> { ...

Is it necessary to continue utilizing PropTypes when incorporating Typescript into a React application?

Typescript brings significant advantages in terms of type validation. Does it completely replace the necessity of using PropTypes? Or do PropTypes still offer unique benefits? ...

create an instance of the class provided as an argument

I wanted to design a system for creating different types of Plants (inspired by plants vs zombies) in an organized way. To simplify the addition of new plant Types, I aimed to make the cost and damage of each plant static so it could be set once for all Pl ...

Having trouble populating the input text field with the selected value from a dropdown using JavaScript

I have implemented a JavaScript function to retrieve the id of a dropdown select with id='odrid'. The script looks like this: $('#odrid').change(function(){ $.getJSON( 'fetch.php', 'odrid='+$(&ap ...

I'm looking to send a response with data using Nest JS API and Postman. How can I accomplish this

As I work on setting up my server using Nest Js, I encountered an issue while trying to fetch data from Postman to test the API urls. Unfortunately, I keep receiving empty responses from the server or undefined values from the postman request. Below is a s ...

Using ngFor to loop through an array of objects and calculate the number of elements in an array property

Currently I am developing a web application using Angular 6. The data is obtained in the form of an array of objects from Firebase, and my intention is to present this information to the user by utilizing ngFor. export interface Competition { createdAt: ...

Exploring the Interaction Between Node.js and a Windows 10 Server on a Local Machine

I am curious about the interaction between Nodejs Server and a local machine. Specifically, I would like to understand how tasks such as: Thread Level CPU Cycle Socket Level IO Any help in clarifying this process would be greatly appreciated. ...

Is it advisable to switch all properties to Angular Signals?

Recently, I've been utilizing signals to replace certain properties in my components that would typically require computed logic or be reactive to the effect hook. It got me thinking - should I be replacing all of my properties with signals, even if t ...

Sharing the checkbox's checked status with an AJAX script

I am faced with a challenge involving a table that contains checkboxes in the first column. When a checkbox is checked, it triggers an AJAX script that updates a PHP session variable with the selected values. The functionality is currently operational, but ...

Transforming vertex coordinates of a polygon using Pixi.js

Greetings! I am a beginner in the world of pixijs (pixi.js - v5.2.4). I recently came across some intriguing examples on the official pixijs website. I decided to experiment by adding a simple slider. By adjusting the slider value, the position of a vertex ...

Tips for displaying a setCustomValidity message or tooltip without needing to submit the event

Currently, I am utilizing basic form validation to ensure that the email entered is in the correct format. Once validated, the data is sent via Ajax for further processing. During this process, I also check if the email address is already in use and whethe ...

Adding up rows and columns using HTML

I've designed an HTML table to function as a spreadsheet, with the goal of being able to total up rows and display the sum in a cell labeled "Total." The same calculation should be applied to columns as well, with totals displayed in the last column c ...

What is the concept of NonNullable in typescript and how can it be understood

In TypeScript, the concept of NonNullable is defined as type NonNullable<T> = T extends null | undefined ? never : T For instance, type ExampleType = NonNullable<string | number | undefined>; Once evaluated, ExampleType simplifies to type Exa ...

What could be causing the page to automatically scroll to the bottom upon loading?

My experience with this bootstrap/jquery page in Firefox (v39, latest updates installed) has been unusual as it always seems to jump to the bottom of the page upon clicking. Upon inspecting the code, I couldn't find any JavaScript responsible for thi ...

Comparison of Angular 2 ViewEncapsulation.Native and ViewEncapsulation.Emulated rendering on Chrome for iPad and desktop

I am currently utilizing an Angular 2 component with the following property, which is functioning perfectly in the Chrome desktop browser. @Component({ selector: 'some-header', templateUrl: './someheader.component.html', styleUr ...

Guide to building a nested dropdown navigation in Angular 12 with the power of Bootstrap 5

I am looking to implement a multilevel dropdown feature in my Angular 12 project with Bootstrap 5. Can someone please guide me on how to achieve this? For reference, you can view an example here. Thank you in advance! ...

Unable to access a frame inside an iframe and frameset using JavaScript when both domains are identical

I am attempting to use JavaScript to access an HTML element within a nested frame in an iframe and frameset. The structure of the HTML is as follows: <iframe id="central_iframe" name="central_iframe" (...)> <frameset cols="185, *" border="0" ...

The watermark feature in HTML may not appear when printed on every page

I'm facing an issue with adding a watermark style. The watermark displays only on the first page when attempting to print in Chrome. It works perfectly in Firefox and IE. <style type="text/css"> #watermark { color: #d0d0d0; font-size: 90pt; ...