This question already has answers here:
Calculating Jday(Julian Day) in javascript
(8 answers)
Closed 8 years ago.
I would like to convert a date such as 2014/09/30 to a Julian date. Converting a date should return an integer.
The reason why I want this integer is to use it in a subtract formula, and then revert the final results back to a date.
How can I convert a date to a Julian and then convert the final results to a date?
Date.prototype.getJulian = function() {
return Math.ceil((this / 86400000) - (this.getTimezoneOffset()/1440) + 2440587.5);
}
var valDate = input1[0];
var dt = new Date(valDate);
var julian_dt = dt.getJulian();
output1 = julian_dt;
i was able to use the code above.
Thanks
You should get familiar with javascript's Date objects. You can subtract one date object from another to find differences in time, and all kinds of other nifty things. It's definitely much cleaner, and less error-prone, than working with strings and integers.
Related
This question already has answers here:
Convert normal date to unix timestamp
(12 answers)
Closed last year.
I know there are numerous ways to go about this, but I'm dealing with date formatted as such:
"2021-01-06T16:24:34Z"
How do I convert this to a timestamp that represent post unix epoch with Javascript?
You just need to parse the date, then divide the resulting number by 1000 to have it in seconds.
To parse it, you just need to remove the T and the Z.
let dateString = "2021-01-06T16:24:34Z";
let dateForDateParsing = dateString.replace("T", " ").replace("Z", "");
console.log(dateForDateParsing);
let UnixTimestamp = Math.floor(new Date(dateForDateParsing) / 1000);
console.log(UnixTimestamp);
new Date("2021-01-06T16:24:34Z").getTime() / 1000
This question already has answers here:
Converting milliseconds to a date (jQuery/JavaScript)
(12 answers)
Closed 1 year ago.
I have seen a variety of threads that are adjacent to this topic. However, my google fu has failed me so far.
I have a value on my server that is a date represented as a string. I am certain it is the milliseconds since the UTC epoch.
I will write it like this for stackOverflow:
const k = '1638400203941'
Date.parse(k); // NaN
const a = parseInt(k, 10);
Date.parse(a); // NaN
// ah ha! but what about...
Date.parse(Date.now() - a); // nope, NaN
I am sure this has been done a thousand times before but how do I do this?
To clarify, the goal is to parse my server's output, k = 1638400203941 into a date. YYYY-MM-DD or MM-DD-YYYY is ok.
edit: Somehow I didn't think to try the given solution. It was giving me Invalid date for some reason, guessing that I was passing in the string version instead of int. My mistake guys.
Just use new Date with you time in numerical format. I used + to convert the string to number.
const k = '1638400203941'
console.log(new Date(+k));
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 1 year ago.
Let's say I have a string 2021-08-13 and want to convert this to August 13, 2021. How would you achieve this as it's not a date object.
In my mind I can think of setting each numeric month to a text version of that month and re-arrange, however seeing if there are better ways of doing this.
Simple: convert the string into a Date object and use the toLocaleString function.
If you want to get rid of the timezone so the date stays the same wherever the user is you can first convert it into an ISO string, get rid of the 'Z' in the end, and then convert it back into the Date object.
const dateString = '2021-08-13'
const localeOptions = {dateStyle: 'long'}
const dateTimezone = new Date(dateString).toLocaleString('en-US', localeOptions)
const dateWithoutTimezone = new Date(new Date(dateString).toISOString().slice(0,-1)).toLocaleString('en-US', localeOptions)
console.log(dateTimezone)
console.log(dateWithoutTimezone)
Convert your string date to a JS Date Object
let strDate = "2021-08-13";
let date = new Date(strDate);
console.log(date.toDateString())
Learn more about Date object here: JavaScript Date
This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Closed 8 years ago.
I want to compare following date in Javascript. Please help me.
$fromdate=2014-12-08
$todate=2014-12-12
I want to compare both the dates with current date. Please tell me how to code in if loop.
You can construct a Date object and then compare them however you want:
// Date months are 0-based
var fromDate = new Date(2014, 11, 8);
var toDate = new Date(); // Today
// Then you can calculate the difference between them
var seconds = (toDate.getTime() - fromDate.getTime())/1000;
var minutes = ~~ (seconds/60);
var hours = ~~ (minutes/60);
var days = ~~ (hours/24);
Then you can use the diff to calculate how many seconds, hours, days etc. there are between them.
A date can be represented numerically as a count of milliseconds from an epoch (01 January, 1970 UTC in javascript).
Create a Date object for today, start and end dates.
Get the numerical representation of these dates by using the method getTime
Now compare today's representation with the start and end representations.
IF the numerical value of today is greater than or equal to the start numerical value, AND
is less than or equal to the end numerical value, THEN
today falls within the supplied range, ELSE
it does not.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Compare 2 dates with JavaScript
Hi,
I'm working on form validation for my application and i want to know where i would be to start looking at to find date comparison eg a start date and an end date. The dates inputted in to my application are in the form: DD/MM/YYYY.
Thanks in Advance,
Dean
If you are using the Javascript Date object, you can simply do:
var startDate = new Date();
var endDate = getEndDate();
if (endDate < startDate)
alert("Houston, we've got a problem!");
EDIT: Changed naming a bit just to stick to camelCase convention, even though I despise it.
this function lets you convert dates to timestamps with wich you could work:
http://caioariede.com/arquivos/strtotime.js
First, you'll want to parse the text into Date objects, then use the language's built-in date comparison. For example:
var dateStr = document.getElementById('foo').value;
var date = Date.parse(dateStr);
var dateStr2 = document.getElementById('foo2').value;
var date2 = Date.parse(dateStr2);
if (date < date2) {
// ...
}