日期格式从Y-m-d更改为2005年9月7日星期三GMT 0530(印度标准时间)
when Page Loads default date 2005-09-01 00:00:00 is loaded , now if i want to change date from date picker to 2010-01-10 then after form submit date changed to Wed Sep 07 2005 00:00:00 GMT 0530 (India Standard Time)
但是我需要日期格式Y-m-d,以便服务器端Php了解日期并将日期保存在数据库中。
我尝试过的解决方案
DatePipe
https://angular.io/api/common/DatePipe#description
const now = Date.now();
const bsValue = this.datePipe.transform(now, 'short');
console.log(bsValue);
其转换日期格式正确。
但是在表单提交日期更改之后。
日期组件代码
import { Component, Input , OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
@Component({
selector: 'date',
template: `
<div [formGroup]="form" >
<input type="text" class="form-control edit-mode-field"
bsDatepicker
[minDate]="minDate"
[maxDate]="maxDate"
[formControlName]="field.code"
[bsConfig]="datePickerConfig"
value="{{ field.value }}"/>
<span class="view-mode-field">{{ field.value }}</span>
</div>
`
})
export class DateComponent implements OnInit {
@Input() field: any = {};
@Input() form: FormGroup;
@Input() type: string;
bsInlineValue = new Date();
datePickerConfig = {
isAnimated: true,
containerClass: 'theme-blue',
showWeekNumbers: false,
dateInputFormat : 'YYYY-MM-DD' ,
};
minDate = new Date();
maxDate = new Date();
constructor( private datePipe: DatePipe , private bsDatepickerConfig: BsDatepickerModule) {
// This is just for tests.
// this.bsDatepickerConfig.dateInputFormat = 'YYYY-MM-DD';
this.minDate.setDate(this.minDate.getDate() - 36135); // 99 years
this.maxDate.setDate(this.maxDate.getDate() + 7);
}
ngOnInit() {
// const ddMMyyyy = this.datePipe.transform(new Date(),"dd-MM-yyyy");
// console.log(ddMMyyyy); //output - 14-02-2019
const now = Date.now();
const bsValue = this.datePipe.transform(now, 'short');
console.log(bsValue);
}
}
接口日期
interface Date {
/** Returns a string representation of a date. The format of the string depends on the locale. */
toString(): string;
/** Returns a date as a string value. */
toDateString(): string;
/** Returns a time as a string value. */
toTimeString(): string;
/** Returns a value as a string value appropriate to the host environment's current locale. */
toLocaleString(): string;
/** Returns a date as a string value appropriate to the host environment's current locale. */
toLocaleDateString(): string;
/** Returns a time as a string value appropriate to the host environment's current locale. */
toLocaleTimeString(): string;
/** Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. */
valueOf(): number;
/** Gets the time value in milliseconds. */
getTime(): number;
/** Gets the year, using local time. */
getFullYear(): number;
/** Gets the year using Universal Coordinated Time (UTC). */
getUTCFullYear(): number;
/** Gets the month, using local time. */
getMonth(): number;
/** Gets the month of a Date object using Universal Coordinated Time (UTC). */
getUTCMonth(): number;
/** Gets the day-of-the-month, using local time. */
getDate(): number;
/** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */
getUTCDate(): number;
/** Gets the day of the week, using local time. */
getDay(): number;
/** Gets the day of the week using Universal Coordinated Time (UTC). */
getUTCDay(): number;
/** Gets the hours in a date, using local time. */
getHours(): number;
/** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */
getUTCHours(): number;
/** Gets the minutes of a Date object, using local time. */
getMinutes(): number;
/** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */
getUTCMinutes(): number;
/** Gets the seconds of a Date object, using local time. */
getSeconds(): number;
/** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */
getUTCSeconds(): number;
/** Gets the milliseconds of a Date, using local time. */
getMilliseconds(): number;
/** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */
getUTCMilliseconds(): number;
/** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */
getTimezoneOffset(): number;
/**
* Sets the date and time value in the Date object.
* @param time A numeric value representing the number of elapsed milliseconds since midnight, January 1, 1970 GMT.
*/
setTime(time: number): number;
/**
* Sets the milliseconds value in the Date object using local time.
* @param ms A numeric value equal to the millisecond value.
*/
setMilliseconds(ms: number): number;
/**
* Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).
* @param ms A numeric value equal to the millisecond value.
*/
setUTCMilliseconds(ms: number): number;
我正在使用https://valor-software.com/ngx-bootstrap/#/datepicker
答案 0 :(得分:0)
日期更改是由于您的前端和后端之间的时区不同
解决方案: -在将日期发送到后端之前,请尝试将其更改为后端服务器的时区 -在前端显示日期时,必须将其转换回浏览器本地时区
Ps:您还可以使用moment.js解析,验证,操作和显示日期