Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 形成日期时不考虑时间的时刻本地化?_Javascript_Angular_Momentjs_Angular10_Moment Timezone - Fatal编程技术网

Javascript 形成日期时不考虑时间的时刻本地化?

Javascript 形成日期时不考虑时间的时刻本地化?,javascript,angular,momentjs,angular10,moment-timezone,Javascript,Angular,Momentjs,Angular10,Moment Timezone,函数格式化日期(localid,inputdate){ var locale='es_MX'; console.log(区域设置、输入日期) //将区域设置为“时刻” 地点(地点); //获取区域设置数据 const localeData=矩.localeData(locale); const format=localeData.longDateFormat('L'); 常数m2=时刻(新日期(输入日期),格式); console.log(m2.format()); console.log(m2

函数格式化日期(localid,inputdate){
var locale='es_MX';
console.log(区域设置、输入日期)
//将区域设置为“时刻”
地点(地点);
//获取区域设置数据
const localeData=矩.localeData(locale);
const format=localeData.longDateFormat('L');
常数m2=时刻(新日期(输入日期),格式);
console.log(m2.format());
console.log(m2.format(format)+使用格式:'+格式);
返回m2.format(格式)
};
日志(格式化日期('ex-MX','2020-10-07T06:02:55Z')

区域设置与时区无关,因为一个区域设置可能属于多个时区。例如,
en-US
有6个时区。因此,我认为传递区域设置不会将日期转换为预期的时区

您可以使用angular提供的格式和时区转换

如果您的日期位于
UST
,则可以通过添加4小时将其转换为
UTC
。然后将其转换为您所需的时区-

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
 zone = 'IST';
 locale = 'en-US'; 
 currentDate = '';
  constructor(private date: DatePipe) {

  }
  getTime() {
    // Date is in UTC (suffix z)
    const d = this.date.transform('2020-10-07T09:30:00Z', 'short', this.zone, this.locale);
    console.log(d);
    this.currentDate = d;
  }
请看

注意

我不确定你是否有可用的时区,但如果你知道它的位置,你总是可以得到它(见答案)

如果您想要基于时区的本地时间,请使用矩时区

   function formatedDate(date,timezone){
      return moment.tz(date,timezone).format('LLLL');
   }

在我们的项目中,我们遇到了很多问题。我们转到了卢克逊。也许这会对你有所帮助。你能将当前结果和预期结果添加到你的问题中吗?@james我会根据要求更新预期输出我需要将maximuz所有单位的日期本地化,所以我使用浏览器导航器获取区域语言代码。语言角度我尝试过,,如何从通用模块导入所有语言局部变量??你能提出确切的问题是什么吗?是时区转换还是从浏览器获取区域设置?请参阅本文,了解如果用户允许,如何获取其位置-如何获取时区?您可以使用矩时区猜测用户时区,也可以使用const clientTimezone=moment.tz.guess(true);您还可以使用const locale=window.navigator.userLanguage | | window.navigator.language;
   function formatedDate(date,timezone){
      return moment.tz(date,timezone).format('LLLL');
   }