How to add a number of days to UTC now in JS? - javascript

I've read a bunch of the related questions but just can't figure this one out.
This is what i have:
var days = "10"; // input is in string
var now = new Date();
var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var newDate = nowUtc.setDate(nowUtc.getDate() + parseInt(days));
The values just don't seem correct so i must be doing it wrong. Can anyone help?
I want the value in the end to be ISO 8601 format.
Thanks!

setDate changes the Date object itself. you can use nowUtc.toISOString() to get ISO 8601 string.
nowUtc.setDate(nowUtc.getDate() + parseInt(days));
var iso = nowUtc.toISOString();

Just use moment.js - a library which simplifies DateTime manipulation in JavaScript.
What you are looking for is Add function and String function.
I hope it helps you!

Related

Get "Day" From This Formatted Timestamp In Javascript

I'm working with Javascript within Google Sheets, and I'm having trouble converting or parsing a formatted timestamp, to ultimately extract the day as a numerical value.
My code:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
Logger.log(date.getDay());
The output:
[19-06-10 17:40:56:107 BST] NaN
My goal is to extract the day number, for example, "18" from that timestamp.
However, it doesn't seem to convert it. I suspect my timestamp isn't in the correct format for the date() function, so it's about creating a function to parse it.
Hopefully, you can help me with that! :) Thank you so much.
The date object has a method like this for getting the day of the month as a number (1-31).
date.getDate();
18 is date.
var shopifyTimestamp ="2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
console.log(date.getDate());
JavaScript's Date constructor supports ISO 8601 date strings. Without using any libraries, you can do something like this:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
// will produce `2019-05-18T13:21:17+0100`
var isoDate = shopifyTimestamp.slice(0, 10)
+ 'T' + shopifyTimestamp.slice(11, 19)
+ shopifyTimestamp.slice(20);
var date = new Date(isoDate);
console.log(date.getDate()); // 18
Also note that you're looking for date.getDate(), rather than date.getDay(). The latter returns the numerical date of the week.

Modifying an ISO Date in Javascript

I'm creating several ISO dates in a Javascript program with the following command:
var isodate = new Date().toISOString()
which returns dates in the format of "2014-05-15T16:55:56.730Z". I need to subtract 5 hours from each of these dates. The above date would then be formatted as "2014-05-15T11:55:56.730Z"
I know this is hacky but would very much appreciate a quick fix.
One solution would be to modify the date before you turn it into a string.
var date = new Date();
date.setHours(date.getHours() - 5);
// now you can get the string
var isodate = date.toISOString();
For a more complete and robust date management I recommend checking out momentjs.
do you have to subtract the hours from a string?
If not then:
var date= new Date();
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();
If you do have to use a string I'd still be tempted to do:
var date= new Date("2014-05-15T16:55:56.730Z");
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();
The way to modify Date objects is via functions such as Date.setHours, regardless of the format you then use to display the date. This should work:
var theDate = new Date();
theDate.setHours(theDate.getHours() - 5);
var isodate = theDate.toISOString();
For complex date operations and enhanced browser compatibility, I highly recommend using moment.js, especially if you're going to be doing several. Example:
var fiveHoursAgo = moment().subtract( 5, 'hours' ).toISOString();
Moment.js is perfect for this. You can do:
var date = new Date();
var newDate = moment(date).subtract('hours', 5)._d //to get the date object
var isoDate = newDate.toISOString();

How to get the date object

My value contains "08.07.1987", how to retrieve the date object for this string. new Date(val) gives correct date object values only for the string value that contains "/" format. can any one let me know hot to create date object for the values which contains "." or "-". in its format.
How about just adjusting the string to suit your needs?
var date1 = new Date("08.07.1987".replace('.','/'));
var date2 = new Date("08-07-1987".replace('-','/'));
You will need to be careful when asking Javascript to interpret a date in this format. As you can probably imagine, a date listed as "08.07.1987" doesn't really specify whether it's August 7th or July 8th.
In general, your best bet will be to specify a date format and parse accordingly.
you have to split the string into tokens for month date and year and then create it using JS Date API.
var date="08.07.1987";
var newDate = date.replace(/(\.|-)/g,"/"));
var dateObject = new Date(newDate);
Replace the delimiters?
var dateStr = "08.07.1987",
dateObj = new Date(dateStr.replace(/[-.]/g,"/"));
Of course you can encapsulate that in a function if need be...
try this new Date("08.07.1987".replace('.','/','g')); tested on firefox only

javascript : how to get 'utc date'

I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.

Javascript Date Object from string in form 'HH-MM'

Hey, just wondering how to convert an HH:MM string into a javascript Date object. I have tried new Date(string); and myDate.setTime() but to no avail.
A side question could be: How to convert a string in HH:MM into milliseconds from Jan 1, 1970.
Thanks for your help in advance.
How about something like:
//using timestr '10:33:21', could also be '10-33-21'
var dat = new Date, time = timestr.split(/\:|\-/g);
dat.setHours(time[0]);
dat.setMinutes(time[1]);
in JavaScript, I'm using the datejs library. http://www.datejs.com/
If you include this library, you have a function called "parseExact" and you could use it like this:
var dateString = "10-12";
var date = new Date.parseExact(dateString, "hh-mm");
To get the miliseconds, you can download the file time.js from http://code.google.com/p/datejs/source/browse/trunk/#trunk/src. Then you have a function getTotalMilliseconds() you can use:
var mSeconds = date.getTotalMilliseconds();
I hope this will help a little bit.

Categories

Resources