How to use .replace to replace the string in a array - javascript

need to replace GMT+0530 (India Standard Time) to IST
dynamically for multiple array list
for now my array list has 6 entries. 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.
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

Related

How to convert date from react calendar in the dd/mm/yyyy format?

I am using react-calendar , Here I am getting a date in the following format
Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)
Now I am trying to convert it to dd/mm/yyyy. is there any way though which I can do this ?
Thanks.
The native Date object comes with seven formatting methods. Each of these seven methods give you a specific value -
toString() : Fri Jul 02 2021 14:03:54 GMT+0100 (British Summer Time)
toDateString(): Fri Jul 02 2021
toLocaleString() : 7/2/2021, 2:05:07 PM
toLocaleDateString() : 7/2/2021
toGMTString() : Fri, 02 Jul 2021 13:06:02 GMT
toUTCString() : Fri, 02 Jul 2021 13:06:28 GMT
toISOString() : 2021-07-02T13:06:53.422Z
var date = new Date();
// toString()
console.log(date.toString());
// toDateString()
console.log(date.toDateString());
// toLocalString()
console.log(date.toLocaleString());
// toLocalDateString()
console.log(date.toLocaleDateString());
// toGMTString()
console.log(date.toGMTString());
// toGMTString()
console.log(date.toUTCString());
// toGMTString()
console.log(date.toISOString());
Format Indian Standard time to Local time -
const IndianDate = 'Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)';
const localDate = new Date(IndianDate).toLocaleDateString();
console.log(localDate);
You could use the methods shown in this blogpost https://bobbyhadz.com/blog/javascript-format-date-dd-mm-yyyy from Borislav Hadzhiev.
You could a new date based on your calendar date and afterwards format it:
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function formatDate(date) {
return [
padTo2Digits(date.getDate()),
padTo2Digits(date.getMonth() + 1),
date.getFullYear(),
].join('/');
}
console.log(formatDate(new Date('Wed Feb 02 2022 00:00:00 GMT+0530 (India Standard Time)')));
This is JavaScript default date format.
You can use libraries like momentjs, datefns, etc to get the result.
For example, if you are using momentjs:-
moment(date).format('dd/mm/yyyy);
Or if you don't want to use any third-party library you can get the result from JavaScript's default date object methods.
const date = new Date();
const day = date.getDate() < 10 ? 0${date.getDate()} : date.getDate();
const month = date.getMonth() + 1 < 10 ? 0${date.getMonth() + 1} : date.getDate() + 1;
const year = date.getFullYear();
const formattedDate = ${day}/${month}/${year};

Javascript: Removing part of a string ( get date string )

I simply need to remove the time from this string Sun Apr 26 2020 01:00:00 GMT+0100 (BST)
Current solution which works
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
const dateTimeArray = dateTime.split(' ');
const date = dateTimeArray.splice(0, 4);
console.log(date.join(' ')); // Correctly returns 'Sun Apr 26 2020'
Although this works I'm wondering if theres a more elegant? Or perhaps a regex?
You can use toDateString
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
console.log(new Date(dateTime).toDateString())
You could try
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
const date = dateTime.substring(0, 15);
console.log(date);
Just use String.substring():
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
console.log(dateTime.substring(0, 15));

javascript - date difference should be zero but it is 18 hours

This one has stumped me. It should be so simple I would think. I am doing some very simple date subtraction in Javascript. I am subtracting the same dates and I would think it would give zero hours, but it gives 18 hours.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
document.getElementById('date').innerHTML = new Date(outDate - inDate);
<div id='date'>
</div>
In case it produces different results based on where you are, the result I am getting is this:
Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
This is due to your timezone. If you convert to GMT String before print it the time will be correct. (Jan 01, 1969 00:00:00)
new Date(outDate - inDate).toGMTString()
You should see the correct date.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
console.log(new Date(inDate - outDate).toGMTString())

How to sort date array with javascript?

I am trying to sort the date array. It does perfectly. But May 4 is listed in the last element of the array which is wrong. Could you help me to resolve this?
var listOfString = "2016-05-24|Failed,2016-05-04|Failed,2016-04-19|Passed,2016-04-15|Passed,2016-06-01|Failed";
var arrayStrings = listOfString.split(',');
var dateArray = new Array();
var count = 0;
for (var i = 0; i < arrayStrings.length; i++) {
dateArray[count++] = new Date(arrayStrings[i].split('|')[0]);
}
dateArray.sort();
for (var i = 0; i < dateArray.length; i++) {
alert(dateArray[i]);
}
Output received as
Fri Apr 15 2016 05:30:00 GMT+0530 (India Standard Time)
Tue Apr 19 2016 05:30:00 GMT+0530 (India Standard Time)
Tue May 24 2016 05:30:00 GMT+0530 (India Standard Time)
Wed Jun 01 2016 05:30:00 GMT+0530 (India Standard Time)
Wed May 04 2016 05:30:00 GMT+0530 (India Standard Time)
Since you have the values as strings in descending order (y-m-d) and with preceding zeroes it means they are sortable also as strings. You don't need to do any conversions in this case, just sort as a string array after split.
Solution:
var listOfString = "2016-05-24|Failed,2016-05-04|Failed,2016-04-19|Passed,2016-04-15|Passed,2016-06-01|Failed";
var arrayStrings = listOfString.split(',');
var dateArray = new Array();
var count = 0;
for (var i = 0; i < arrayStrings.length; i++) {
dateArray[count++] = arrayStrings[i].split('|')[0];
}
dateArray.sort();
for (var i = 0; i < dateArray.length; i++) {
console.log(dateArray[i]);
}
Change your date.sort() to this
dateArray.sort(function(a,b) {
return a-b;
});
Returns
VM126:57 Fri Apr 15 2016 05:30:00 GMT+0530 (IST)
VM126:57 Tue Apr 19 2016 05:30:00 GMT+0530 (IST)
VM126:57 Wed May 04 2016 05:30:00 GMT+0530 (IST)
VM126:57 Tue May 24 2016 05:30:00 GMT+0530 (IST)
VM126:57 Wed Jun 01 2016 05:30:00 GMT+0530 (IST)
You assumed that .sort() would automatically handle dates but unfortunately, that's not the case. It either does alphabetical or numerical sort only and that was the result you had. To fix, I added a callback function that will determine if an item is ahead or behind in the order in the bubble sort and subtracting dates returns the number of seconds between the two dates and that would be a number and easy to subtract and return the difference in the sort.

How to Format JavaScript Long String Date

I am using this code to export dates of current week days
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toString());
}
but the output looks like Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time) in the array of
["Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov
09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015
01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57
GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800
(Pacific Standard Time)"]
Can you please let me know how I can format the date() to get only
Mon Nov 09 2015 and remove 01:43:57 GMT-0800 (Pacific Standard Time)?
Thanks
You can use the toDateString() method on the Date object.
arr.push(next.toDateString());
Its simple. I am using your code. just use toDateString() method of Date()
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toDateString());
}
document.write(new Date().toDateString());

Categories

Resources