以UTC日期时间为本地时间的Angular(v4)DatePipe

时间:2018-12-04 22:13:23

标签: angular datetime iso8601

与更新版本的Angular不同,Angular 4 DatePipe似乎不支持将时区作为参数传递。

我有一个后端服务,该服务返回一个没有时区的ISO 8601格式的日期时间字符串(例如def decoding_layer(dec_embed_input, embeddings, enc_output, enc_state, vocab_size, text_length, summary_length, max_summary_length, rnn_size, vocab_to_int, keep_prob, batch_size, num_layers): '''Create the decoding cell and attention for the training and inference decoding layers''' for layer in range(num_layers): with tf.variable_scope('decoder_{}'.format(layer)): lstm = tf.contrib.rnn.LSTMCell(rnn_size, initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2)) dec_cell = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob = keep_prob) output_layer = Dense(vocab_size, kernel_initializer = tf.truncated_normal_initializer(mean = 0.0, stddev=0.1)) attn_mech = tf.contrib.seq2seq.BahdanauAttention(rnn_size, enc_output, text_length, normalize=False, name='BahdanauAttention') dec_cell = tf.contrib.seq2seq.AttentionWrapper(dec_cell, attn_mech, rnn_size) initial_state = tf.contrib.seq2seq.AttentionWrapperState(enc_state[0],_zero_state_tensors(rnn_size, batch_size, tf.float32)) with tf.variable_scope("decode"): training_logits = training_decoding_layer(dec_embed_input, summary_length, dec_cell, initial_state, output_layer, vocab_size, max_summary_length) with tf.variable_scope("decode", reuse=True): inference_logits = inference_decoding_layer(embeddings, vocab_to_int['<GO>'], vocab_to_int['<EOS>'], dec_cell, initial_state, output_layer, max_summary_length, batch_size) return training_logits, inference_logits ),并以UTC格式存储并返回。

如何使用2018-12-04T19:01:50.062将日期时间显示为用户的本地时间?

1 个答案:

答案 0 :(得分:3)

Angular 4的DatePipe支持UTC zone designator后缀('Z'),因此,如果您传递给管道的值是UTC日期时间,则后端服务可以使用'Z返回日期时间'区域指示符,或者您可以附加DatePipe,它将以用户的本地时间显示日期时间。

例如,

Z

注意:,如果您希望<span class="date">{{ value + 'Z' | date:'short' }}</span> 使用用户的Angular的LOCALE_ID以正确的语言环境(将导出的提供程序导入到您的应用模块中),则需要确保该设置。浏览器设置:

DatePipe

export class WindowRefService { public getWindow(): Window { return window; } } export function languageFactory(windowRef: WindowRefService): string { return windowRef.getWindow().navigator.language; } export const localeProvider = { provide: LOCALE_ID, deps: [WindowRefService], useFactory: languageFactory }; 上查看其他问题:Angular4: Locale for a user