最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ionic ion-datetime - default it to the date and time with the timezone of the user's device - Stack Overflo

programmeradmin0浏览0评论

I'm using Ionic 3. I used ion-datetime ponent:

myDate: String = new Date().toISOString();

<ion-datetime displayFormat="HH:mm" pickerFormat="HH:mm" [(ngModel)]="myDate"></ion-datetime>

I want to have today's date that depends on the timezone of the user device. Unfortunately new Date().toISOString() always returns the GMT one. any advises how to this properly ?

I'm using Ionic 3. I used ion-datetime ponent:

myDate: String = new Date().toISOString();

<ion-datetime displayFormat="HH:mm" pickerFormat="HH:mm" [(ngModel)]="myDate"></ion-datetime>

I want to have today's date that depends on the timezone of the user device. Unfortunately new Date().toISOString() always returns the GMT one. any advises how to this properly ?

Share Improve this question asked Aug 8, 2017 at 20:16 MelchiaMelchia 24.4k23 gold badges108 silver badges129 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

If you use toLocalString() it gives you the following format MM/DD/YYYY, HH:MM:SS A

If you need the a ISO 8601 format consider using the below code:

mydate: String = (new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000)).toISOString().slice(0, -1);

try using the toLocaleString() function of the Javascript Date Object.

 myDate: String = new Date().toLocaleString();

Hope this helps.

  1. Open console at root proyect and install moment: npm install moment --S.
  2. Import moment in ponent file: import moment from 'moment';.
  3. Set value of model variable: this.myDate = moment().format().

Check this post for more details: https://stackoverflow./a/47843362/7829826

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论