Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to Date in Angular2 \ Typescript? [duplicate]

I want to create a new Date object, with a specific date. I thought of converting it from a specific string, for example:

let dateString = '1968-11-16T00:00:00'

How can I convert it to a date object in typescript?

Update:

I'm asking for a solution in Typescript and not in Javascript, and in Angular2, not AngularJS (1.x)

like image 375
cookya Avatar asked Apr 04 '17 08:04

cookya


People also ask

How do I convert a string to a Date in TypeScript?

Use the Date() constructor to convert a string to a Date object in TypeScript, e.g. const date = new Date('2024-07-21') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How do I convert a string to a Date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How convert dd mm yyyy string to Date in Javascript?

To convert a dd/mm/yyyy string to a date:Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.

How to convert string to date in angular?

To convert the String to the Date filter, you can use the date filter that helps convert the Date and displays it in a specific format. Let’s consider the following example in which the DatePipe method is used, an Angular method that formats a date value according to locale rules.

How to convert a string to a date object in typescript?

Use the Date () constructor to convert a string to a Date object in TypeScript, e.g. const date = new Date ('2024-07-21'). The Date () constructor takes a valid date string as a parameter and returns a Date object. We used the Date () constructor to convert a string to a Date object.

How to add a timestamp in angular applications?

In angular applications, many ways we can do it In the typescript, timestamp variable is declared with value In component template html filter, using pipe symbole, with date attribute, date format is declared.

How to convert date to string date format in JavaScript?

First, we will create sample new date () object variable, after then, we will use .toString () method to convert it on string date format. let’s dive in code… Well, when you run above code, you can able to see date converted in string date format.


1 Answers

You can use date filter to convert in date and display in specific format.

In .ts file (typescript):

// variables used in html
let dateString = '1968-11-16T00:00:00' 
let newDate = new Date(dateString);

// format date in typescript
getFormatedDate(date: Date, format: string) {
    const datePipe = new DatePipe('en-US');
    return datePipe.transform(date, format);
}

// Convert date to user timezone
const localDate: Date = this.convertToLocalDate('01/01/2021');

convertToLocalDate(responseDate: any) {
    try {
        if (responseDate != null) {
            if (typeof (responseDate) === 'string') {
                if (String(responseDate.indexOf('T') >= 0)) {
                    responseDate = responseDate.split('T')[0];
                }
                if (String(responseDate.indexOf('+') >= 0)) {
                    responseDate = responseDate.split('+')[0];
                }
            }

            responseDate = new Date(responseDate);
            const newDate = new Date(responseDate.getFullYear(), responseDate.getMonth(), responseDate.getDate(), 0, 0, 0);
            const userTimezoneOffset = newDate.getTimezoneOffset() * 60000;

            const finalDate: Date = new Date(newDate.getTime() - userTimezoneOffset);
            return finalDate > Util.minDateValue ? finalDate : null;
        } else {
            return null;
        }
    } catch (error) {
        return responseDate;
    }
}

In HTML:

{{dateString |  date:'MM/dd/yyyy'}}

Below are some formats which you can implement :

Backend:

public todayDate = new Date();

HTML :

<select>
<option value=""></option>
<option value="MM/dd/yyyy">[{{todayDate | date:'MM/dd/yyyy'}}]</option>
<option value="EEEE, MMMM d, yyyy">[{{todayDate | date:'EEEE, MMMM d, yyyy'}}]</option>
<option value="EEEE, MMMM d, yyyy h:mm a">[{{todayDate | date:'EEEE, MMMM d, yyyy h:mm a'}}]</option>
<option value="EEEE, MMMM d, yyyy h:mm:ss a">[{{todayDate | date:'EEEE, MMMM d, yyyy h:mm:ss a'}}]</option>
<option value="MM/dd/yyyy h:mm a">[{{todayDate | date:'MM/dd/yyyy h:mm a'}}]</option>
<option value="MM/dd/yyyy h:mm:ss a">[{{todayDate | date:'MM/dd/yyyy h:mm:ss a'}}]</option>
<option value="MMMM d">[{{todayDate | date:'MMMM d'}}]</option>   
<option value="yyyy-MM-ddTHH:mm:ss">[{{todayDate | date:'yyyy-MM-ddTHH:mm:ss'}}]</option>
<option value="h:mm a">[{{todayDate | date:'h:mm a'}}]</option>
<option value="h:mm:ss a">[{{todayDate | date:'h:mm:ss a'}}]</option>      
<option value="EEEE, MMMM d, yyyy hh:mm:ss a">[{{todayDate | date:'EEEE, MMMM d, yyyy hh:mm:ss a'}}]</option>
<option value="MMMM yyyy">[{{todayDate | date:'MMMM yyyy'}}]</option> 
</select>
like image 166
Sandip - Frontend Developer Avatar answered Oct 22 '22 16:10

Sandip - Frontend Developer