I want to extract "10:30" from mystring and convert it to "10, 30".
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
Output will be:
Time: 10:30
Changed Format: 10, 30
Provide code solution only.
You can use this to extract the time:
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
var match = mystring.match(/(\d+:\d+):\d+/);
if (match) {
var output = "Time: " + match[1].replace(":", ", ");
}
Related
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));
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
here how can i convert the
[Sun Jul 15 2018 17:48:13 GMT+0530 (India Standard Time), Sun Jul 22 2018 17:48:13 GMT+0530 (India Standard Time)]
to 20180715 using javascript
here a variable named DateData is storing the above two dates
DateData:Date[];
After selecting the from the datetime picker i storing the data in the DateData
now i am trying to convert the variable using DateData.toISOstring or DateData.toDate() also not working displaying as unable to convert the dataData to Date Format
Perhaps something like this
function sqlDate(dat)
{
return dat.toISOString().substr(0,10).replace('-','');
}
There you go. :)
function convertDates(dateArr) {
var newDateArray = [],
dateObj;
for (var i=0; i<dateArr.length; i++) {
dateObj = new Date(dateArr[i]);
newDateArray.push(dateObj.getFullYear() + '' + dateObj.getMonth() + '' + dateObj.getDate());
}
return newDateArray;
}
var dateArr = ['Sun Jul 15 2018 17:48:13 GMT+0530 (India Standard Time)', 'Sun Jul 22 2018 17:48:13 GMT+0530 (India Standard Time)'];
console.log(convertDates(dateArr));
function formatDate(date){
if(date instanceof Date) date = new Date(date);
var fullZero = s => s.toString().length ===1 ?'0'+s :s.toString();
return `${date.getFullYear()}${fullZero(date.getMonth()+1)}${fullZero(date.getDate())}`;
}
formatDate(new Date) // -> "20180722"
If you have an array of Date, use dateData.map(formatDate) to format it.
Try splitting on the "T" like this snippet:
var d = new Date();
// => d.toISOString() show 2019-02-01T06:38:21.990Z
console.log(d.toISOString().split("T")[0].replace(/-/g, ''));
or you can use a JavaScript library called date-and-time for the purpose.
let now = date.format(new Date(), 'YYYYMMDD');
console.log(now);
<script src="https://cdn.jsdelivr.net/npm/date-and-time/date-and-time.min.js"></script>
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 can I convert this format "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)" to just 2014-01-31 in Javascript ?? I know it should be simple but I didnt get it from google
var d = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var str = $.datepicker.formatDate('yy-mm-dd', d);
alert(str);
http://jsfiddle.net/3tNN8/
This requires jQuery UI.
jsFiddle Demo
Split the string based on the blank spaces. Take the parts and reconstruct it.
function convertDate(d){
var parts = d.split(" ");
var months = {Jan: "01",Feb: "02",Mar: "03",Apr: "04",May: "05",Jun: "06",Jul: "07",Aug: "08",Sep: "09",Oct: "10",Nov: "11",Dec: "12"};
return parts[3]+"-"+months[parts[1]]+"-"+parts[2];
}
var d = "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)";
alert(convertDate(d));
You can do it like this
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var year=date.getFullYear();
var month=date.getMonth()+1 //getMonth is zero based;
var day=date.getDate();
var formatted=year+"-"+month+"-"+day;
I see you're trying to format a date. You should totally drop that and use jQuery UI
You can format it like this then
var str = $.datepicker.formatDate('yy-mm-dd', new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
I found Web Developer's Notes helpful in formatting dates
For things like this it's often good to do a little testing in the browser console.
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
console.log(date.getFullYear() + '-' + date.getMonth()+1 + '-' + date.getDate())
Ensure you add + 1 to the result of getMonth() because it is zero based.
A similar question was asked here:
Where can I find documentation on formatting a date in JavaScript?
start_date="14 Feb 2020";
var new_startDate= new Date(start_date);
var date= moment(new_startDate).format('YYYY-MM-DD');
Answer: 2020-02-14
In here you have to use moment.js
The easiest way to convert is
new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: '2-digit'
}).format(new Date('Your Date'))
Just Replace 'Your Date' with your complicated date format :)
You can also use the Moment.js library, make sure to give it a search.
Few examples: moment().format('MMMM Do YYYY, h:mm:ss a');
moment().format('dddd');
A trifling refinement:
var date = new Date(value);
var year = date.getFullYear();
var rawMonth = parseInt(date.getMonth()) + 1;
var month = rawMonth < 10 ? '0' + rawMonth : rawmonth;
var rawDay = parseInt(date.getDate());
var day = rawDay < 10 ? '0' + rawDay : rawDay;
console.log(year + '-' + month + '-' + day);