Javascript : Increase a Date object by a year - javascript

I'm assign a new date object to my object attribute like that :
giftObject.purshasedDate = new Date()
which give a date format :
Date Thu Feb 20 2020 13:36:37 GMT+0100 (heure normale d’Europe
centrale)
I want to increase this date by one year, I tried :
new Date().setFullYear(giftObject.purshasedDate.getFullYear() + 1) but it give a number serial like this : 1613824899244
I do not understand what that number serial mean! it's a date or should a try some thing else ?

By default all dates object are timestamps.
JavaScript Date objects represent a single moment in time in a
platform-independent format. Date objects contain a Number that
represents milliseconds since 1 January 1970 UTC.
Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
I think the default new Date() object can display itself to string by in fact it's also a timestamp.
If you want to display a date as string, you have to use the toLocaleString() method on Date.
I tried by updating the original date and it return the string of the date, don't know why but it's work by updating the original date.
Example :
let giftObject = {};
giftObject.purshasedDate = new Date();
giftObject.purshasedDate.setFullYear(giftObject.purshasedDate.getFullYear() + 1);
console.log(giftObject.purshasedDate)
Result : "20/02/2021 à 13:55:49" for my French browser

const oldDate = new Date("Date Thu Feb 20 2020 13:36:37 GMT+0100")
const newDate = oldDate.setFullYear(oldDate.getFullYear() + 1)
const dateWithPlusOneYear = new Date(newDate)
console.log(new Date(dateWithPlusOneYear))
//Sat Feb 20 2021 13:36:37 GMT+0100 (Central European Standard Time)

Please use this one:
purshasedDate = new Date();
purshasedDate = new Date(purshasedDate.setFullYear(purshasedDate.getFullYear() + 1));

Related

How to change ISO date to Standard JS Date?

I am trying to change an ISO date to a Standard JS Date format. The JS format I am referring to is:
Mon `Jul 20 2020 14:29:52 GMT-0500 (Central Daylight Time)`
What is the best way to go about doing this? Thanks!
const ISO_DATE = '2020-07-14T23:02:27.713Z';
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toString();
};
console.log(formatDate(ISO_DATE));
One way is:
let isoDate = "2020-07-20T14:29:52Z";
var myDate = new Date(isoDate);
console.log(myDate.toString()); // Mon Jul 20 2020 17:29:52 GMT+0300 ( (your time zone)
console.log("Back to ISO Date: ", myDate .toISOString());
If you want to convert it back to ISO Date use:
console.log(myDate.toISOString());

How to convert a Date object to output only hh:mm am/pm

I am attempting to convert the following into a 12 hour am/pm format.
Currently I am recieving the Day, Month, Year and timezone.
Fixed by adding .toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.)/, "$1$3")*
<div id="time1"></div>
<div id="time2"></div>
var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;
var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;
Basically what you have to do is use the Date() default javascript function and make sure you append the UTC timezone:
var date = new Date('08/16/2019 7:00:00 PM UTC')
date.toString=() //will then print out the timezone adjusted time
"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"
There are many built in javascript methods to handle converting date objects. This example will look to the browser to determine date format and time.
let time = Date.now();
time.toLocaleDateString();

Javascript - Determine date format based on timezone

I have two date strings and a timezone (which will vary for each user). With this information I need to construct two date objects.
//information I have
var date1 = '05/05/2018'
var date2 = '06/05/2018'
var timezone = 'Australia/Sydney'
//date objects
var date1 = new Date(date1); // Sat May 05 00:00:00 GMT+00:00 2018
var date2 = new Date(date2); // Tue Jun 05 00:00:00 GMT+00:00 2018
Problem
date2 should be 6th of May (rather than 5th of June).
Since I have the timezone, is there a javascript function that will allow me to pass the date along with the timezone and it to automatically determine the correct date format (e.g dd/mm or mm/dd)?
I think the simple way to work with momentjs,
You can find example like this:
var a = moment.tz("2013-11-18 11:55", "America/Toronto");
var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
var c = moment.tz(1403454068850, "America/Toronto");

How to manipulate date in javascript

I'm having problem with manipulation of dates. I have a variable savedTime variable in localstorage wich contains this date:
Wed Aug 31 2016 16:31:30 GMT-0300 (Hora oficial do Brasil)
I need add 1 hour for this variable savedTime to check if passed 1 hour:
var savedTime = new Date(savedTime); //converts string to date object
var checkExpired = savedTime.setHours(savedTime.getHours() + 1); //add 1 hour to savedTime
But on trying add 1 hour to this variable converting the string in a object, this (savedTime) returns:
1472675490000
What i expected is the string with + 1 hour:
Wed Aug 31 2016 17:31:30 GMT-0300 (Hora oficial do Brasil)
And compare dates to check if passed 1 hour
var currentDate = new Date();
if(currentDate > checkExpired) {
//passed 1 hour
}
instance.setHours() manipulates the instance. So you can do
d = new Date('Wed Aug 31 2016 16:31:30 GMT-0300');
d.setHours(d.getHours() + 1);
Now d contains the new datetime.
setHours will manipulate the current object and return it's new value. Instead of using the return value, just continue to use the object.
var savedTime = new Date(savedTime); //converts string to date object
savedTime.setHours(savedTime.getHours() + 1); //add 1 hour to the savedTime
if (currentDate > savedTime) {
//passed 1 hour
}
Use .getTime()
if(currentDate.getTime() > checkExpired) {
//passed 1 hour
}
You are getting the value back in milliseconds from the 'epoch date' which is January first 1970.
If you want this in the correct format, you will need to parse this information out into that with some math / other Date object functions.
Here is the documentation to do that, it's very straightforward:
JS Date Formatting

Unexpected Date result from using Number and Split

Why does the following code:
newDate = "2-24-2014";
var splitDate = newDate.split('-');
var dateObj = new Date(Number(splitDate[0]), Number(splitDate[1]) - 1, Number(splitDate[2]));
Produce the following?:
Sat Jun 05 1909 00:00:00 GMT+0100 (GMT Daylight Time)
I know the formatting but not the strange date itself. I was wondering if it had something to do with Number but cant seem to find any answers on this.
new Date(Year, Month, Date)
The above is the actual format. Whereas you have given like new Date(Month, Date, Year)
var dateObj = new Date(Number(splitDate[2]), Number(splitDate[0]) - 1, Number(splitDate[1]));
the problem is the order of parameters:
new Date(Number(splitDate[2]), Number(splitDate[0]) - 1, Number(splitDate[1]));
or simply do this:
new Date(Date.parse("2-24-2014"))

Categories

Resources