本文介绍了如何在Angular2 \ Typescript中将字符串转换为Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个带有特定日期的新Date对象.我想将其从特定的字符串转换,例如:
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?
更新:
我要在Typescript中而不是在Javascript中以及 Angular2 中而不是AngularJS(1.x)中寻求解决方案
I'm asking for a solution in Typescript and not in Javascript, and in Angular2, not AngularJS (1.x)
推荐答案
您可以使用日期过滤器转换日期并以特定格式显示.
You can use date filter to convert in date and display in specific format.
在.ts文件(打字稿)中:
let dateString = '1968-11-16T00:00:00'
let newDate = new Date(dateString);
在HTML中:
{{dateString | date:'MM/dd/yyyy'}}
下面是一些您可以实现的格式:
Below are some formats which you can implement :
后端:
public todayDate = new Date(Date.parse(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>
这篇关于如何在Angular2 \ Typescript中将字符串转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!