I have date like this 25. 02. 2014 18:48:21 and I'm trying to convert it into timestamp
var someDate = '25. 02. 2014 18:48:21';
var timestamp = new Date(someDate).getTime();
but it's returning NaN since I moved files to a new domain, what can be a problem?
'25. 02. 2014 18:48:21' is not a valid date format. You'll have to convert it with regex first, like that:
var someDate = '25. 02. 2014 18:48:21';
var converted = someDate.replace(/^(\d{2}\. )(\d{2}\. )(\d{4})/, '$3. $2$1');
// converted is in format: YYYY. MM. DD.
var timestamp = new Date(converted).getTime();
Running this within the console, creating a new date with that variable gives me Invalid Date. Trying switching around the 25. and 02. like so:
var someDate = '02. 25. 2014 18:48:21';
var timestamp = new Date(someDate).getTime(); // 1393372101000
The format should be "Month, Day, Year, Time".
Switching month and day will work. I also removed the dots.
var date = "25. 02. 2014 18:48:21";
new Date(date.replace(/(\d{2})\. (\d{2})\./, '$2 $1'))
// Tue Feb 25 2014 18:48:21 GMT+0100 (W. Europe Standard Time)
you can try something like below (if your string has always same format)
var someDate = '25. 02. 2014 18:48:21';
var arr = someDate.split(' ');
var time = arr[3].split(':');
var timeStamp = new Date(arr[2],arr[1].split('.')[0],arr[0].split('.')[0],time [0],time[1],time[2]).getTime();
It uses javascript date object constructor
var d = new Date(year, month, day, hour, minute, seconds);
which works across all browsers
function convertSomeDate(str){
var d= str.match(/\d+/g),
dA= [d[2], '-', d[1], '-', d[0], 'T', d[3], ':', d[4], ':', d[5], 'Z'].join('');
return +new Date(dA)
}
var someDate= '25. 02. 2014 18:48:21';
convertSomeDate(someDate)
/* returned value: (Number)
1393354101000
*/
Related
How to get date time from this /Date(1518696376959)/ in javascript?
I have tried like this
var d = new Date("1519192874994");
I have tried like this var d = new Date("1519192874994");
No need to wrap it in quotes, Date constructor will takes millisecond value (Number)
var d = new Date(1519192874994) //Wed Feb 21 2018 11:31:14 GMT+0530 (India Standard Time)
From "/Date(1518696376959)/"
Make it
var str = "/Date(1518696376959)/";
var date = new Date( +str.match(/\d+/g)[0] ); //notice the unary plus after getting the match
i want to get date and time from this kind of timestamp string
var t = "2017-10-28 10:46:20".split(/[- :]/);
// Apply each element to the Date function
var d = new Date(Date.UTC(t[0], t[1]-1, t[2], t[3], t[4], t[5]));
console.log(d);
I want my desired result to be like below :
Sat Oct 28 2017 at 10:46 am
You could do this in vanilla js however I would use the Moment library which makes working with any type of date formatting super simple.
In your example you would just need to do this
const timeStamp = '2017-10-28 10:46:20';
const date = moment(timeStamp).format('ddd MMM do YYYY [at] HH:mm:ss A');
//date => 'Sat Oct 2017 at 10:46:20 AM'
I have a form set-up in such a way that I collect the dates from a different input box and time from a dif
var appointment_date = new Date();
var appointment_start = new Date("Mon Apr 24 2017 20:00:00 GMT-0400 (EDT)");
var appointment_end = new Date("Mon Apr 24 2017 21:30:00 GMT-0400 (EDT)");
console.log(appointment_date);
console.log(appointment_start);
console.log(appointment_end);
let selected_day = appointment_date.toString().split(' ').slice(0, 1).join(' '),
selected_date = appointment_date.toString().split(' ').slice(1, 2).join(' '),
selected_month = appointment_date.toString().split(' ').slice(2, 3).join(' '),
selected_year = appointment_date.toString().split(' ').slice(3, 4).join(' ');
console.log(selected_day);
console.log(selected_date);
console.log(selected_month);
console.log(selected_year);
console.log(appointment_start.toString().split(' ').slice(4, appointment_start.toString().length).join(' '));
console.log(new Date(selected_year, selected_month, selected_date, appointment_start.toString().split(' ').slice(4, appointment_start.toString().length).join(' ')));
ferent input field.
I tried converting my Date and Time to string; splitting them and then joining it with the other time but it's not giving me the proper time. Is there a better way to do this?
where this.state.appointment_date = new Date();
this.state.appointment_start = new Date();
this.state.appointment_end = new Date();
let selected_day = this.state.appointment_date.toString().split(' ').slice(0, 1).join(' '), // Day
selected_date = this.state.appointment_date.toString().split(' ').slice(1, 2).join(' '), // Date
selected_month = this.state.appointment_date.toString().split(' ').slice(2, 3).join(' '), // Month
selected_year = this.state.appointment_date.toString().split(' ').slice(3, 4).join(' '); // Year
console.log(this.state.appointment_start.setDate(selected_day)); //NaN (output)
console.log(this.state.appointment_start.toString().split(' ').slice(4, this.state.appointment_start.toString().length).join(' ')); // no output
console.log(this.state.appointment_end.toString().split(' ').slice(4, this.state.appointment_end.toString().length).join(' ')); //time
// I tried to create a new date using the above date:
console.log(new Date(selected_day, selected_month, selected_date, selected_year, this.state.appointment_start.toString().split(' ').slice(4, this.state.appointment_start.toString().length).join(' ')));
But I get invalid date for it. Not sure how to do it :/
This way I was trying to break it up and then combine it again to create the final date. But this seems like a really long approach for something which should be simpler
Expected Input/Output:
Input: dates in the same format as new Date()
Output: Take day, month and year from appointment_date and time from appointment_start and create a new date object
The correct syntax of Date is
new Date(year, month, day, hours, minutes, seconds, milliseconds);
so you mess the supplied params.
The selected_day in your case will produce the name of the day.
In your code:
var appointment_date = new Date();
var appointment_start = new Date("Mon Apr 24 2017 20:00:00 GMT-0400 (EDT)");
var appointment_end = new Date("Mon Apr 24 2017 21:30:00 GMT-0400 (EDT)");
You should not parse strings with the Date constructor (or Date.parse) as it's largely implementation dependent. The standard does not require a string in this format to be parsed correctly. If you want to create some test dates, use:
var appointment_start = new Date(2017, 3, 24, 20);
assuming that your time zone is -0400. The first part of your code can be rewritten as:
var appointment_date = new Date();
var appointment_start = new Date(2017, 3, 24, 20);
var appointment_end = new Date(2017, 3, 24, 21, 30);
console.log(appointment_date.toString());
console.log(appointment_start.toString());
console.log(appointment_end.toString());
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var selected_day = days[appointment_date.getDay()],
selected_date = appointment_date.getDate(),
selected_month = appointment_date.getMonth() + 1,
selected_year = appointment_date.getFullYear();
console.log(selected_day);
console.log(selected_date);
console.log(selected_month);
console.log(selected_year);
let selected_day = appointment_date.toString().split(' ').slice(0, 1).join(' '),
This depends on the output of Date.prototype.toString having a particular format, however ECMA-262 allows the format to be implementation dependent, so it may have a different format in different hosts. See Where can I find documentation on formatting a date in JavaScript?
Consider using a library, or write your own small formatter if you only have one format to support.
Where you have:
console.log(this.state.appointment_start.setDate(selected_day)); //NaN (output)
You are passing a value like "Tue" (or maybe something completely different) instead of a date, so the result is NaN.
In the last line you have:
console.log(new Date(selected_year, selected_month, selected_date,
appointment_start.toString().split(' ').slice(4,
appointment_start.toString().length).join(' ')));
Which attempts to build a Date by passing values for the first three parameters, but then tries to pass all the parts for the time as a single string. You need to pass all the parts as individual values. Also, months are zero indexed so you need to subtract 1.
But there is no need for such convoluted code, all the parts of the date are available using Date methods, see Where can I find documentation on formatting a date in JavaScript? as noted above.
I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.
How can I strip out everything except for the actual date?
If you want to keep using Date and not String you could do this:
var d=new Date(); //your date object
console.log(new Date(d.setHours(0,0,0,0)));
-PS, you don't need a new Date object, it's just an example in case you want to log it to the console.
http://www.w3schools.com/jsref/jsref_sethours.asp
Like this:
var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
dateString = new Date(dateString).toUTCString();
dateString = dateString.split(' ').slice(0, 4).join(' ');
console.log(dateString);
I'm using this workaround :
// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())
You could use Moment.js, a library that provides many helper functions to validate, manipulate, display and format dates and times in JavaScript.
Using Moment.js lib:
var dateString = new Date('Mon Jan 12 00:00:00 GMT 2015');
moment(dateString).format('YYYY-MM-DD HH:mm');
Or simplified:
moment('Mon Jan 12 00:00:00 GMT 2015').format('YYYY-MM-DD HH:mm')
Well,
Here is my Solution
let dateString = 'Mon May 25 01:07:00 GMT 2020';
let dateObj = new Date(dateString);
console.log(dateObj.toDateString());
// outputs Mon May 25 2020
See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
Just cut it with substring:
var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);
In this case you can just manipulate your string without the use of a Date object.
var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
date = dateTime.split(' ', 4).join(' ');
document.body.appendChild(document.createTextNode(date));
You can first convert the date to String:
String dateString = String.valueOf(date);
Then apply substring to the String:
dateString.substring(4, 11) + dateString.substring(30);
You need to take care as converting date to String will actually change the date format as well.
How can I convert a string representation of a date to a real javascript date object?
the date has the following format
E MMM dd HH:mm:ss Z yyyy
e.g.
Sat Jun 30 00:00:00 CEST 2012
Thanks in advance
EDIT:
My working solution is based on the accepted answer. To get it work in IE8, you have to replace the month part (e.g. Jun) with the months number (e.g. 5 for June, because January is 0)
Your date string can mostly be parsed as is but CEST isn't a valid time zone in ISO 8601, so you'll have to manually replace it with +0200.
A simple solution thus might be :
var str = "Sat Jun 30 00:00:00 CEST 2012";
str = str.replace(/CEST/, '+0200');
var date = new Date(str);
If you want to support other time zones defined by their names, you'll have to find their possible values and the relevant offset. You can register them in a map :
var replacements = {
"ACDT": "+1030",
"CEST": "+0200",
...
};
for (var key in replacements) str = str.replace(key, replacements[key]);
var date = new Date(str);
This might be a good list of time zone abbreviation.
You can use following code to convert string into datetime:
var sDate = "01/09/2013 01:10:59";
var dateArray = sDate.split('/');
var day = dateArray[1];
// Attention! JavaScript consider months in the range 0 - 11
var month = dateArray[0] - 1;
var year = dateArray[2].split(' ')[0];
var hour = (dateArray[2].split(' ')[1]).split(':')[0];
var minute = (dateArray[2].split(' ')[1]).split(':')[1];
var objDt = new Date(year, month, day, hour, minute);
alert(objDt);