121

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)

5
  • 2
    @AravindSivam I asked in typescript, not javascript Commented Apr 4, 2017 at 14:09
  • @GünterZöchbauer I tryed, didn't find anything relevant (abviously) Commented Apr 4, 2017 at 14:10
  • @GünterZöchbauer please notice I was asking for a solution in Typescript (not Javascript) for Angular2 (not AngularJS 1.x which is very different). You can read it both in the question title or in my update. Commented Apr 4, 2017 at 14:40
  • 2
    JavaScript is a subset of TypeScript. Not everything needs to be different just because you're using TypeScript ;-) Commented Apr 4, 2017 at 14:44
  • 1
    @cookya Typescript is a superset of javascript. There is not separate method for typescript Commented Apr 4, 2017 at 15:31

1 Answer 1

274

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>
Sign up to request clarification or add additional context in comments.

3 Comments

@sandip-full-stack-developer, I'm curious why you use todayDate = new Date(Date.parse(Date())) instead of todayDate = new Date(). Both will return different result?
This doesn't appear to show how to format a date with a format string on backend? i.e "'MM/dd/yyyy'"
@SomeRandomDeveloper I have updated logic to convert from typescript using datepipe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.