React Native new Date() not working - javascript

I tried to get Date from datestring but it's not wokring properly.
var time = new Date('2017-12-26T02:12:00')
But when I called time.getHours() it returns 12.
I am not sure what I am doing wrong.

Use below code to get day, month and year.
var date = new Date(),
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear(),
today = day+"-"+month+"-"+year;
You can change the format of date as per your requirement.

Adding below code fixed the problem
time.setMinutes(time.getMinutes() + time.getTimezoneOffset());
:)

Related

Error incrementing days to a specific date

I'm trying to increment one day to a given date. My code, inspired by this answer, looks like:
var date = getDateFromUIControl();
var backupDate = new Date();
backupDate.setDate(date.getDate() + 1);
However, I'm seeing a strange behaviour. Today is December 5th, 2019. If the user selects January 1, 2020 (stored in date variable), then backupDate ends up being January 2nd, 2019, instead of 2020. What is wrong with this code? How should I go about incrementing the date, if what I'm doing is wrong?
Note: because of whatever policies my company has, I can't use any JavaScript library other than jQuery.
new Date() returns the current Date(example: 05/12/2019). You are just changing the date alone in current date. Still the year is 2019.
it should be like,
date.setDate(date.getDate() + 1);
if you can't change the original date object, then it can be done like this,
var changedDate = new Date(date);
changedDate.setDate(changedDate.getDate() + 1);
var date = getDateFromUIControl();
var backupDate = new Date();
backupDate.setDate(new Date(date).getDate() + 1);
nextDay is one day after date:
var date = getDateFromUIControl();
var nextDay = new Date(date.getYear(), date.getMonth(), date.getDate()+1);
Also you don't need to worry about overflowing d.getDate()+1 (e.g. 31+1) - the Date constructor is smart enough to go into the next month.

how to get day format from new Date in Angularjs

i want to get the value of the day format from new Date()(current date) in my angularjs projet. I try this code in my javascript file:
var today = (new Date()).toISOString();
console.log(today.getDay());
when running my code, i get this message error :
TypeError: today.getDay is not a function
however there are many solutions with this syntax.
How can i fix it please. Any help is appreciated
Use getDay on the Date object not on the ISO string:
var today = (new Date()).getDay();
getDay returns a value from 0(Sunday) to 6(Saturday).
If you want current date and day according to your timezone then ->
var today = new Date().getDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getDate()
If you want current date and day according to UTC timezone then ->
var today = new Date().getUTCDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getUTCDate()
You can get date by using below code
let dayno = new Date(this.date.getFullYear(), this.date.getMonth() ,20).getDay();<br>
if date is 20-11-2019 then Day No is :3

converting date of format 23,59,59,997 to next day in java script

I have an EndDate in millisecond and the way it is saved in DB is of format 2015-06-11 23,59,59,997. If i want to automatically add a startDate (endDate + 1) in millisecond, which is of format 2015-06-12 00,00,00,000. what is the best approach I can follow in javascript. Should i add the remaining milliseconds to the EndDate to calculate the StartDate ? If so how much millisecond is should add? or should I add 84600000ms with the EndDate and setHours(0,0,0,0).? Any help will be appretiated
With something like this:
function roundDate (msUTC) {
var proxyDate = new Date()
proxyDate.setTime(msUTC)
return (new Date(
proxyDate.getUTCFullYear(),
proxyDate.getUTCMonth(),
(proxyDate.getUTCDate() + 1)
))
}
roundDate(235959997).toString()
Just create a new date from the old date and add one to the day only.
var mil=86400000;
var d = new Date(mil);
var newd=new Date(d.getYear(),d.getMonth(),d.getDate()+1);
console.log(newd.toString());

momentJS date string add 5 days

i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.
here my javascript Code:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);
alert(new_date);
here my jsfiddle: http://jsfiddle.net/jbgUt/1/
How can i solve this ?
I like this string format "25.03.2014"
Hope someone can help me.
UPDATED: January 19, 2016
As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)
var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
Thanks #Bala for the information.
UPDATED: March 21, 2014
This is what you'd have to do to get that format.
Here's an updated fiddle
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(day + '.' + month + '.' + year);
ORIGINAL: March 20, 2014
You're not telling it how/what unit to add. Use -
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))
has to format and then convert to moment again.
The function add() returns the old date, but changes the original date :)
startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
You can add days in different formats:
// Normal adding
moment().add(7, 'days');
// Short Hand
moment().add(7, 'd');
// Literal Object
moment().add({days:7, months:1});
See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/
var end_date = moment(start_date).clone().add(5, 'days');
If we want to use the current date or present date:
var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);
To get an actual working example going that returns what one would expect:
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
add https://momentjs.com/downloads/moment-with-locales.js to your html page
var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date
then
var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..
point 2 and 3 are using in your jquery code...
If you do end up running with formatting problems after adding X time to the function, try this format:
startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");
instead of:
startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");
This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.
You can reduce what they said in a few lines of code:
var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
updated:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
alert(new_date)

Add to current date with javascript and have months roll over

I need to use basic javascript here and I'm not well versed with it.
I do not want to use jquery (although I would prefer it)
I need to get the current date via javascript, add 2 days to it, then display the new date (with the 2 additional days factored in). This means on the 30 or 31st of the month, the month needs to rollover as does the year on Dec 30/31.
Thanks to this question and Samuel Meadows I can get the current date. I can add 2 days no problem. But I can't work out how to get the months (and year) to rollover properly.
Samuel's code:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = mm+'/'+dd+'/'+yyyy;
document.write(today);
Any assistance would be greatly appreciated.
Thanks!
The date object will take care of this for you:
var date = new Date("March 30, 2005"); // get an edge date
date.getMonth(); // 2, which is March minus 1
date.setDate( date.getDate() + 2); //Add two days
date.getMonth(); //Now shows 3, which is April minus 1
Convert the number of days to milliseconds, and then add them to the date in question http://jsfiddle.net/Mn5Wz/
Here is the code to add days to the current date
​var today = new Date();
console.log(addDate(today, 2));
function addDate(dateObject, numDays) {
dateObject.setDate(dateObject.getDate() + numDays);
return dateObject.toLocaleDateString();
}

Categories