使用moment.js和moment-timezone.js时,获取客户端时区并将其转换为其他时区的最佳方法是什么?
我想找出什么是客户时区,然后将其日期和时间转换为其他时区.
有没有人有这方面的经验?
我如何使用moment.js提取时间?
"2015-01-16T12:00:00"
Run Code Online (Sandbox Code Playgroud)
它应该返回"12:00:00 pm".字符串返回将传递给下面的timepicker控件.
http://jdewit.github.com/bootstrap-timepicker
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我意识到,这个问题是重复但前一次没有提供适当的答案时刻包已经安装
1.安装包裹
npm install moment-timezone --save
在node_modules目录中
|
| --moment
| --moment-timezone
目录存在
2. Index.html包含脚本
<script src="node_modules/moment-timezone/moment-timezone.js"></script>
Run Code Online (Sandbox Code Playgroud)
System.config.js
var map = {
'moment': 'node_modules/moment',
'momentzone': 'node_modules/moment-timezone'
};
var packages = {
'moment': { defaultExtension: 'js' },
'momentzone': { defaultExtension: 'js' }
};
Run Code Online (Sandbox Code Playgroud)
3.Inside component.ts文件
import * as moment from 'moment/moment';
export class TimeComponent implements OnInit{
ngOninit(){
console.log(moment("2014-06-01T12:00:00Z").tz('America/Los_Angeles').format('ha z'));
}
}
Run Code Online (Sandbox Code Playgroud)
应该导入什么来防止错误 属性tz在"Moment"类型上不存在
从服务器收到的日期是在UTC时区,我需要将其转换为特定的时区,例如:America/New_York.Florlowing是相同的代码
<span class="bold" ng-bind="sess.date_time | amTimezone:'America/New_York' | amDateFormat:'h:mm a'"></span>
Run Code Online (Sandbox Code Playgroud)
但是这样做我得到以下错误:
Moment Timezone has no data for America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
Run Code Online (Sandbox Code Playgroud)
但这America/New_York是一个已知的时区,moment但仍然要求我添加时区.
我moment.js用来更改应用程序的本地日期格式,但出现以下错误:
导入库时,“ moment”没有导出的成员“ default”。
下面是我的代码:
import {Inject, Injectable, Optional} from '@angular/core';
import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material';
import * as _moment from 'moment';
import {default as _rollupMoment, Moment} from 'moment';
const moment = _rollupMoment || _moment;
Run Code Online (Sandbox Code Playgroud) 我使用该am-time-ago指令来显示相对时间戳:
<span am-time-ago="publishedAt"></span>
Run Code Online (Sandbox Code Playgroud)
默认情况下,它被格式化为"一天前","5天前"等.
如何将格式更改为"1d","5d","3h"等?
因此,我使用的是 Angular 材质日期选择器,它只能帮助我选择月份和年份。 https://material.angular.io/components/datepicker/overview#watching-the-views-for-changes-on-selected-years-and-months
但是,除非您执行以下操作,否则该功能不会按预期工作 - date = new FormControl(moment()); (除了月份和年份之外,您还可以选择日期。)
现在,我们知道 moment() 只返回当前日期和时间。我的主要目标是在应用程序的表单中显示现有用户详细信息。但是,用户可以在加载表单后对其进行编辑。这些字段之一是材料日期选择器。现在,它不是必填字段,所以我并不总是从后端收到有效的响应。当响应是日期时,绝对没有问题。但是每当我收到一个空值时,就会出现主要问题。
我查看了 Moment.js 文档(https://momentjs.com/docs/),它明确指出 moment(null) 和 moment("") 都是无效日期。因此,一旦我使用两者中的任何一个来初始化日期选择器,日期选择器就根本不允许我在可编辑模式下选择日期。
如何使用不会干扰日期选择器功能的 moment() 设置有效的空日期?
我有一个ng-repeat,显示以下表达式
{{date}} // 2-1-2017
Run Code Online (Sandbox Code Playgroud)
当我使用角度矩
{{date | amDateFormat:'DD'}}
Run Code Online (Sandbox Code Playgroud)
我得到1,我期待2.如何让我知道我的格式实际上dd-mm-yyyy不在mm-dd-yyy视图中?我不想在我的控制器级别解析它,因为它很复杂.
我按照英雄之旅教程,我现在正在修改项目.我的项目将有很多时钟和计时器,我的第一个任务是制作一个显示UTC时间的时钟.使用MomentModule,我可以显示页面加载的时间:
<p>{{ myDate | amDateFormat: 'ddd Do HH:mm:ss' }}</p>
Run Code Online (Sandbox Code Playgroud)
但是,它并不像我想要的那样每秒更新一次.
我的DashboardComponent看起来像这样:
export class DashboardComponent implements OnInit {
heroes: Hero[] =[];
myDate: Date;
constructor(private heroService: HeroService) {}
ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes);
this.utcTime();
}
utcTime(): void {
setInterval(function() {
this.myDate = new Date();
console.log(this.myDate); // just testing if it is working
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,创建新的Date()是个好主意; 每一秒?无论哪种方式,有没有办法通过像观察或类似的东西每秒更新我的视图中的时间?就像我说的那样,当我完成时,我的页面上会有很多计时器和时钟.谢谢!
我将这样的日期保存在我的mongo数据库中:2018年10月25日星期四17:30:03 GMT + 0300(EAT)。我想使用momentjs在1小时前或3小时前的前端使用。我将如何处理?
angular-moment ×10
momentjs ×8
angular ×4
javascript ×4
angularjs ×3
angular7 ×2
datetime ×2
node.js ×1
time ×1
timezone ×1
typescript ×1