简体   繁体   English

如何使用 .replace 替换数组中的字符串

[英]How to use .replace to replace the string in a array

need to replace GMT+0530 (India Standard Time) to IST dynamically for multiple array list对于多个数组列表,需要将 GMT+0530(印度标准时间)动态替换为 IST

for now my array list has 6 entries.现在我的数组列表有 6 个条目。 need to replace for all the array list .需要替换所有数组列表。

function getTimeAccordingtoTimeZone(utc){
    utc  = new Date(Date.parse(utc));
    var dateUTC = utc ;
    var dateIST = new Date(dateUTC);
    //date shifting for IST timezone (+5 hours and 30 minutes)
    var current_time_zone = getCurrentTimeZone();
    var hour_diff = parseInt(current_time_zone);
    var minute_diff = current_time_zone - hour_diff;
    minute_diff = minute_diff*60;
    dateIST.setHours(dateIST.getHours() + hour_diff);
    dateIST.setMinutes(dateIST.getMinutes() + minute_diff);
    var new_date = dateIST;
    return new_date;
}
new_date returns
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)

I'd propose you use moment JS to format the string.我建议您使用 moment JS 来格式化字符串。

In your case, the following code will help you:在您的情况下,以下代码将帮助您:

const moment = require('moment');
date = moment();
const dateString = `${date.format('ddd MMM DD YYYY HH:mm:ss')} IST`
console.log(dateString);

MomentJs Documentation MomentJs文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM