Convert UTCString to yyyy-mm-dd - javascript

I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.
var newDate = this.getCellDate(target);
console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
cstDate = newDate.toISOString();
console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**

Use Date.UTC() method
var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDay(),
hours = now.getHours(),
minutes = now.getMinutes(),
utcDate;
utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))
Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));
Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)
Fiddle example

Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString
custdate = newDate.toLocaleDateString();
dueDate= custdate.split("/").reverse().join("-");

Related

subtracting two dates via moment results in wrong year

Hey folks running into a problem with dates that I would love an extra set of eyes on. Any help would be appreciated.
Problem: I have a future event which will occur on a certain date, when I subtract the future event date from the current date I get a year that is in the past and an extra day.
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat).utc();
const now = moment().utc();
const timeDiff = moment(futureEvent - now).utc(); // also tried futureEvent.diff(now)
let daysLeft: moment(timeDiff).format('D') - 1; // have to subtract 1
console.log('futureEvent:', futureEvent);
// Sun Dec 08 2019 00:00:00 GMT-0700 (Mountain Standard Time)
console.log('now:', now);
// Wed Dec 04 2019 10:18:26 GMT-0700 (Mountain Standard Time)
console.log('timeDiff:', timeDiff);
// Sun Jan 04 1970 06:41:33 GMT-0700 (Mountain Standard Time)
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat);
const now = moment();
const timeDiff = moment.duration(futureEvent.diff(now));
let daysLeft = timeDiff.asDays();
const disp = document.getElementById("display");
disp.innerText = daysLeft.toFixed(); // 3.0123
console.log("futureEvent:", futureEvent.toString());
Same code i just get rid of .utc()s. used .duration() and for getting dates used .asDays().
Pen

How to set time with AM PM in Javascript Date Object

Say that I have DateTime in this format Fri Feb 02 2018 00:00:00 GMT+0530 (IST)
And from the time picker plugin getting the time 1:10am or 2:30pm in this format.
I am not sure how to calculate and combine/add them both to produce this result:
Fri Feb 02 2018 01:10:00 GMT+0530 (IST) or Fri Feb 02 2018 14:30:00 GMT+0530 (IST)
I wish if there was something to do as simple as this:
new Date(dateString).setHours(1:10am)
Seems like you need to parse it on your own:
function parseDaytime(time) {
let [hours, minutes] = time.substr(0, time.length -2).split(":").map(Number);
if (time.includes("pm") && hours !== 12) hours += 12;
return 1000/*ms*/ * 60/*s*/ * (hours * 60 + minutes);
}
To add it to a date:
new Date(
+new Date("Fri Feb 02 2018 00:00:00 GMT+0530")
+parseDaytime("1:20pm")
);
Here is a simple function to do what your after.
It basically splits the time using a regex, and then calls setHours & setMins, adding 12 hours if pm is selected.
The example below takes the current datetime, and sets 1:10am & 2:40pm..
function setHours(dt, h) {
var s = /(\d+):(\d+)(.+)/.exec(h);
dt.setHours(s[3] === "pm" ?
12 + parseInt(s[1], 10) :
parseInt(s[1], 10));
dt.setMinutes(parseInt(s[2],10));
}
var d = new Date();
console.log(d);
setHours(d, "1:10am");
console.log(d);
setHours(d, "2:40pm");
console.log(d);
You can parse the time string into hours & minutes, adjust the hours according to am/pm & set it to the date object then:
var dateString = 'Fri Feb 02 2018 00:00:00 GMT+0530 (IST)';
var hoursString = '2:30pm';
var parts = hoursString.replace(/am|pm/, '').split(':')
var hours = parseInt(parts[0]) + (hoursString.indexOf('pm') !== -1 ? 12 : 0);
var minutes = parts[1];
var date = new Date(dateString);
date.setUTCHours(hours, minutes);
console.log(date); // in your local time
console.log(date.toUTCString()); // in UTC (i.e. without timezone offset)
(Note setHours / setUTCHours mutates date object but returns unix timestamp of the updated datetime.)

Date list using javascript simple

Hello guys could give me a little help ?
Code here https://jsfiddle.net/pedrowperez/hgb5ufqw/2/`
this is my work.
HTML
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<p id="demo5"></p>
<p id="demo6"></p>
<p id="demo7"></p>
Javascript
var someDate = new Date();
var Ano_Confirmado = someDate.getFullYear();
var Mes_Confirmado = someDate.getMonth();
var Dia_Confirmado = someDate.getDate();
var Week = 7;
var Day = 1;
var someDateD = new Date(Ano_Confirmado, Mes_Confirmado, (Dia_Confirmado) - Week);
document.getElementById('demo1').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo2').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo3').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo4').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo5').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo6').innerHTML = (someDateD);
someDateD.setDate(someDateD.getDate() + Day);
document.getElementById('demo7').innerHTML = (someDateD);
Result
Tue Jan 26 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Wed Jan 27 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Thu Jan 28 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Fri Jan 29 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Sat Jan 30 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Sun Jan 31 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Mon Feb 01 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Tue Feb 02 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
Wed Feb 03 2016 00:00:00 GMT-0200 (E. South America Daylight Time)
I need to change the date format : Wed Mar 23 2016 15:19:36 GMT-0300 (E. South America Standard Time)
for this format: DD/MM
But I can not find anything Referring dates will list in javascript;
There are two options that come to mind:
Use the Date methods, though this is relatively limiting
Use a library like Moment.js.
Using Date methods
someDate.getDay() + '/' + (someDate.getMonth() + 1);
The (someDate.getMonth() + 1) is required because getMonth() returns the zero-indexed month. Meaning January is 0, February is 1 and so on.
Using Moment.js
Look into a library like . Using Moment you can format dates in endless ways:
moment(someDateD).format('DD/MM');
Here's your updated JSFiddle.
You can create your own function that handles it if you don't need any other complex manipulations with dates.
function formateDate(date) {
function addZero_(num) {
return num < 10 ? ('0' + num) : num);
}
var day = date.getDate();
var month = date.getMonth() + 1;
return addZero_(day) + '/' + addZero_(month);
}
document.getElementById('demo3').innerHTML = formateDate(someDateD);
Also use for to set date values in dom.

how to add days to datetime in javascript?

I have date and time format datetimepicker. I would like to add number of days to the datetime
inputdattime will be picked from datetimepicker
input datetime = Friday, October 23rd 2015, 12:00:00 am
output should be if i add 2 days sunday, oct 25 2015 12:00:00 am
how to achieve this
var deal_from_date = new Date();
var numberOfDaysToAdd = 7;
var date1 = deal_from_date.setDate(deal_from_date.getDate() + numberOfDaysToAdd);
actual Output should be
"dddd, MMMM Do YYYY, h: mm:ss a"
Sun Oct 25 2015 12:00:00
current output is:
Sun Oct 25 2015 12:54:06
setDate() method can be used to add days to a date
var today=new Date('23/10/2015');
var in_a_day=new Date(today).setDate(today.getDate()+2);
Try this code :-
var d = new Date('oct 25 2015 12:00:00 pm');
d.setDate(d.getDate() + 2)
alert(d);
output is :- Tue Oct 27 2015 12:00:00 GMT+0530 (IST)

How to set Hours,minutes,seconds to Date which is in GMT

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?
var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Expected result is
Fri, 26 Sep 2014 00:00:00 GMT
How Do I achieve ?
According to MDN the setHours function actually takes additional optional parameters to set both minutes, seconds and milliseconds. Hence we may simply write
// dateString is for example "Fri, 26 Sep 2014 18:30:00 GMT"
function getFormattedDate(dateString) {
var date = new Date(dateString);
date.setHours(0, 0, 0); // Set hours, minutes and seconds
return date.toString();
}
You can use this:
// Like Fri, 26 Sep 2014 18:30:00 GMT
var today = new Date();
var myToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
Recreate the Date object with constructor using the actual date.
To parse the date into JavaScript simply use
var date = new Date("Fri, 26 Sep 2014 18:30:00 GMT”);
And then set Hours, Minutes and seconds to 0 with the following lines
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.toString() now returns your desired date

Categories