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"))
Related
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));
I have the following date: 07/08/2018 in the format m/d/Y. This date is july 08, 2018.
And I have the following JavaScript code:
var dateFrom = '07/08/2018';
dateFrom = new Date(dateFrom);
alert(dateFrom);
When I do this, I get the following result:
Sun Jul 08 2018 00:00:00 GMT+0200 (Midden-Europese zomertijd)
As you can see, because I live in Belgium, I get the time + GMT+2. But that's not what I want. I want the exact UNIX timestamp of 07/08/2018 (or any other date) of GMT+0.
I have the following JavaScript code:
var dateFrom = '07/08/2018';
dateFrom = Math.floor((new Date(dateFrom)).getTime()/1000);
alert(dateFrom);
If I execute this code, I get the following result:
1531000800
But that's not what I want. If I check the UNIX timestamp I get (1531000800) on this (https://www.unixtimestamp.com/index.php) website, I get the following result:
1531000800
Is equivalent to:
07/07/2018 # 10:00pm (UTC)
I want the UNIX timestamp that is equal to 07/08/2018 # 00:00am (UTC).
How can I achieve this?
Thanks in advance!
As per the MDM documentation:
The following statement creates a Date object using UTC instead of local time:
var utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0));
Code Sample:
var date = new Date()
var utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
console.log(date)
console.log(utcDate)
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");
I have following datetime value in a json :
Fri Jan 22 2016 14:34:38 GMT-0500
I would like to display something like "January 22, 2016"
How could I achieve this in javascript. I have JQuery, Extjs libraries available.
Try creating object having properties of abbreviated months, values of full month, using for..in loop , String.prototype.slice(), String.prototype.replace()
var months = {
"Jan":"January",
"Feb":"February",
"Mar":"March",
"Apr":"April",
"May":"May",
"Jun":"June",
"Jul":"July",
"Aug":"August",
"Sep":"September",
"Oct":"October",
"Nov":"November",
"Dec":"December"
};
var date = "Fri Jan 22 2016 14:34:38 GMT-0500";
// extract "Jan 22 2016" from `date`
var d = date.slice(4, -18);
for (var prop in months) {
if (new RegExp(prop).test(d)) {
// replace abbreviated month with full month name
d = d.replace(prop, months[prop]);
// replace day with day followed by comma `,` character
d = d.replace(/(\d{2})(?=\s)/, "$1,")
}
}
document.body.textContent = d
This question is addressed to the same question of yours. You can use the functions shown here to construct the date string as you want.
// This could be any Date String
var str = "Fri Feb 08 2013 09:47:57 GMT +0530 (IST)";
var date = new Date(str);
This will then give you access to all the Date functions (MDN)
For example:
var day = date.getDate(); //Date of the month: 2 in our example
var month = date.getMonth(); //Month of the Year: 0-based index, so 1 in our example
var year = date.getFullYear() //Year: 2013
Extract date and time from string using Javascript
Found this crazy method here, but worked!
Converting milliseconds to a date (jQuery/JS)
Here is the fiddle that i'v done
https://jsfiddle.net/Ripper1992/hj6L2Lvz/
var now = new Date("Fri Jan 22 2016 14:34:38 GMT-0500");
alert(now.customFormat( "#MMMM# #DD#, #YYYY#" ) );
customFormat is the function called to get each part of the Data, parse and replace based on the #MMMM# or #DD# or #SS# defined by the user.
And here is the complete function with the documentation
http://phrogz.net/JS/FormatDateTime_JS.txt
In the following date conversion after converting back the long integer The date says october instead of september
var date = 2013-09-23 18:31
startdate = getTimeStamp(date); //1382533260000
Now
t=1382533260000
rt = new Date(t)
//Wed Oct 23 2013 18:31:00 GMT+0530 (India Standard Time)
function getTimeStamp(strDate) {
var a1=strDate.split(" ");
var d1=a1[0].split("-");
var t1=a1[1].split(":");
var dtObj = new Date(d1[0],d1[1],d1[2],t1[0],t1[1]);
return dtObj.getTime();
}
In JavaScript, month numbers are numbered 0-11.
If you're parsing from components like this into the Date constructor you'll have to subtract one from the number:
function getTimeStamp(strDate) {
var a1=strDate.split(" ");
var d1=a1[0].split("-");
var t1=a1[1].split(":");
var dtObj = new Date(d1[0],d1[1] - 1,d1[2],t1[0],t1[1]);
return dtObj.getTime();
}
Months are zero-based, so January is zero, February is one, etc..
So you need to use d1[1]-1 in your new Date() constructor.
Javascript month parameter starts from 0 upto 11 so, passing 8 means september