javascript date and year - javascript

I have the following javascript code:
<script type="text/javascript">
$(function () {
var currentDateTime = new Date();
var oneYear = new Date();
oneYear.setYear(oneYear.getYear() + 1);
alert(currentDateTime + "_" + oneYear);
});
</script>
i would expect the alert to output the current datetime and the datetime of one year from now. However I get this in the alert: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)"
Clearly it's not adding "1" to the Year correctly!
Whats going on? How did it become the year 0111???

It is correct. .getYear() returns "actual year − 1900". 2010 − 1900 = 110.
Use .getFullYear() instead. .getYear() has been deprecated for a long time.

Y2K was 10 years ago, but you're still using getYear instead of getFullYear? tsk tsk...
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getFullYear

Instead of .getYear() try .getFullYear()

Related

How do I add one day or 12 hours to date in format of Thu Mar 03 2022 12:00:00 GMT in JavaScript [duplicate]

This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed last year.
I want to add one day or 12 hours to a date in the format of
Thu Mar 03 2022 12:00:00 GMT
I have tried:
new Date(value.startDate + 1);
but it does nothing.
Please help me out, I am new to JavaScript.
If you want to add something to your timestamp, this will do the trick no matter what you want to add.
const timestamp = new Date("Thu Mar 03 2022 12:00:00 GMT")
console.log(timestamp.toString())
// because getTime and setTime uses milliseconds:
const millisecondsToBeAdded = 12 * 60 * 60 * 1000
timestamp.setTime(timestamp.getTime() + millisecondsToBeAdded)
console.log(timestamp.toString())
Try this
const date = new Date("Thu Mar 03 2022 12:00:00 GMT");
date.setDate(date.getDate() + 1);

JS setHours on date is not working correctly

I have the following code to increment the hours in a date:
let timerExpireDate = new Date(countdownStartDate);
console.log(`original date is ${timerExpireDate}`);
console.log(`add on ${countdownHours} hours`);
timerExpireDate.setHours(timerExpireDate.getHours() + countdownHours);
console.log(`New date is ${timerExpireDate}`);
However it also seems to be incrementing the days by 6, here is the console log:
original date is Sun Jul 19 2020 16:36:39 GMT+0800 (Taipei Standard Time)
add on 2 hours
New date is Sat Jul 25 2020 18:36:39 GMT+0800 (Taipei Standard Time)
What am I doing wrong here?
It is likely that countdownHours is of type string instead of number, so timerExpireDate.getHours() + countdownHours is '162' (6 days later) instead of 18.
The fix is to cast countdownHours to number first, like countdownHours = +countdownHours.

Wrong result when converting a date

I am just a beginner. My original date is: Friday, September 16th 2016, 09:00
And I need to convert it to this format: Fri Sep 16 2016 09:00:00 GMT+0000 (GMT)
But my code shows wrong date: Tue Sep 06 2016 09:00:00 GMT+0100 (BST)
Please see my code here:
var startdate = "Friday, September 16th 2016, 09:00";
var sdate = new Date(startdate.replace(/(\d)+(st|nd|th)/g, '$1'));
alert(new Date(sdate));
Can anybody help me with this issue? The example is here: https://jsfiddle.net/5hzwbbku/3/
By using momentjs I receive the strange results: https://jsfiddle.net/5hzwbbku/13/
Your regex is incorrect. /(\d)+(st|nd|th)/g matches the 16th but the captured group only contains 6. In order to return 16 your regex needs to be /(\d+)(st|nd|th)/g, like so:
var sdate = new Date(startdate.replace(/(\d+)(st|nd|th)/g, '$1'));
If you need the time to be in UTC, you'll have to append a timezone such as +0, +0000, or simply Z (for Zulu time).
var startdate = "Friday, September 16th 2016, 09:00";
startdate = startdate + ' +0000';
var sdate = new Date(startdate.replace(/(\d+)(st|nd|th)/g, '$1'));
alert(new Date(sdate));
On my system that returns Fri Sep 16 2016 11:00:00 GMT+0200 (CEST) which is 09:00 in UTC.
Other regexes that match correctly are /(\d)(st|nd|th)/g (note the complete absence of the +) and the one given by pastine in the comments /(.\d)+(st|nd|th)/g.

How to Convert Hard Coded date into standard GMT format

I need to convert a hard coded date into a standard GMT format.How can I do this?
The date I have is in the following format:
var myDate = 'dd|mm|yyyy';
There is no time or day description in the date.Just the 'dd|mm|yyyy' string.
Is there a way I can convert it into GMT?
Thanks in advance.
a = '22/02/2014'.split('/')
d = new Date(a[2],parseInt(a[1], 10) - 1,a[0])
//Sat Feb 22 2014 00:00:00 GMT+0530 (India Standard Time)
Now you have a javascript date object in d
utc = d.getUTCDate() + "/" + (d.getUTCMonth() + 1 ) + "/" + d.getUTCFullYear();
//"21/2/2014" for an accurate conversion to UTC time of day is a must.
If you are in say India, the Javascript Date object will have timeZoneOffset 330. So its not possible to keep a javascript Date object with timezone GMT unless your system time is GMT.
So if you want a Date object for calculation, you can create one with localTimezone and simply suppose it is GMT
pseudoGMT = new Date( Date.parse(d) + d.getTimezoneOffset() * 60 * 1000);
//Fri Feb 21 2014 18:30:00 GMT+0530 (India Standard Time)
If you can explain your high level requirement we might be able to help with some alternate solutions.
Use regex matching to extract the data you need:
var myDate = "21|01|2014";
var data = myDate.match(/(\d{2})\|(\d{2})\|(\d{4})/);
var date = new Date(data[3], data[2] - 1, data[1]);
Note that the month is 0-indexed, so january = 0
More on regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Format date time JavaScript

How to format date and time like this in JavaScript ?
March 05, 2012 # 14:30 (UTC - 9:30)
I use this code to calculate EST time :
function getDate() {
var now = new Date();
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * -4));
}
I use the date-time-format that Tats recommended because doing it manually is a huge PIA.
var yourDate = dateFormat(getDate(), "mmmm dd, yyyy # HH:MM) + "(UTC -9:30)";
Keep in mind this isn't Daylight Savings aware.. and you are asking for UTC -9:30 in your format, but your function converts to -4. Also, I believe that now.getTime returns in UTC.. so you can just add your difference there.
JavaScript Date Format
Check out date.js! It's a really powerful little library for working with Dates in JavaScript.
To get today's date in EST, you can do something like...
var today = new Date();
today.toString(); // outputs "Wed Apr 11 2012 15:40:40 GMT-0500 (CDT)"
today.setTimezone("EST");
today.toString(); // outputs "Wed Apr 11 2012 14:40:40 GMT-0500 (CDT)"
Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.
If you do just
var now = new Date();
document.write(now);
you will get
Wed Mar 14 2012 20:53:06 GMT+0000 (GMT Standard Time)
Link1, Link2.
Is it what you want?

Categories