convert string into datetime object in javascript - javascript

I have to send Date object for current date to my back end from javascript
What I am doing is
var currentDate = new Date();
var dateString = currentDate.getMonth() + "-"
+ currentDate.getDate() + "-" + currentDate.getFullYear() + " "
+ currentDate.getHours() + ":" + currentDate.getMinutes() + ":"
+ currentDate.getSeconds();
var newDate = new Date(Date.parse(dateString));
But it is saying Invalid Date to newDate.
I have to send 3-10-2013 6:10:25 PM as datetime object to backend.

var currentDate = new Date(),
utcYear = currentDate.getUTCFullYear(),
utcMonth = ('0' + currenctDate.getUTCMonth()).slice(-2),
utcDay = ('0' + currentDate.getUTCDate()).slice(-2),
fullDateString = utcMonth.toString() + '/' + utcDay.toString() + '/' + utcYear.toString();
Same principle if you want to get the time part as well.

Instead of putting - in between the month/date and date/year, just put spaces.
var currentDate = new Date(),
dateString = currentDate.getMonth() + " " +
currentDate.getDate() + " " +
currentDate.getFullYear() + " " +
currentDate.getHours() + ":" +
currentDate.getMinutes() + ":" +
currentDate.getSeconds(),
newDate = new Date(dateString);
console.log(newDate)

Related

UTC timestamp conversion to selected timezone timestamp

this.selectedTimezone="Pacific/Kiritimati";
//this value will come from dropdown
this.records = data.body; //api response
for (var i = 0; i < this.records.length; i++)
{
var d = new Date(this.records[i]['startTimeStamp']);
//it is converting to system timezone date format. I want this one to selected timezone conversion.
var t1 = moment(d).tz(this.selectedTimezone).format();
//2018-11-23T05:30:00+14:00
}
I want this t1 in full date format i.e Friday November 30, 2018 09:00:00
See Moment and Moment-timezone.
// This will get "Pacific/Kiritimati" current timezone.
var datetime = moment().tz("Pacific/Kiritimati").format();
var timestamp = moment().tz("Pacific/Kiritimati").valueOf();
console.log(datetime);
console.log(timestamp);
<script src="https://rawgit.com/moment/moment/2.22.2/min/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>
var d1= moment.tz(new Date(this.records[i]['startTimeStamp']), this.selectedTimezone);
var key = d1.year() + "-" + ('0' + (d1.month() + 1)).slice(-2) + "-" + ('0' + (d1.date())).slice(-2);
var timeSlot = ('0' + d1.hour()).slice(-2) + ":" + ('0' + d1.minutes()).slice(-2);
example:-
var d1= moment.tz(new Date(2543622499000), "Pacific/Kiritimati");
var key = d1.year() + "-" + ('0' + (d1.month() + 1)).slice(-2) + "-" + ('0' + (d1.date())).slice(-2);
var timeSlot = ('0' + d1.hour()).slice(-2) + ":" + ('0' + d1.minutes()).slice(-2);
output:-2050-08-09 15:48

Date parse format from T with time to mm/dd/yyyy with javascript or jquery

I have the following value:
javascript variable:
"2015-10-14T17:54:19.033"
I want to end up with
mm/dd/yyyy
e.g.
10/14/2015
I was trying to do
var date = month[d.getMonth()] + " " + d.getDay()+ ", " + d.getFullYear();
Here is my Fiddle
http://jsfiddle.net/bthorn/7ptcedt4/
var d = new Date('2015-10-14T17:54:19.033');
var date = d.getMonth().toString() + "/" + d.getDay().toString() + "/" + d.getFullYear().toString();
console.log(date);
Something is NOT right: I am getting
9/3/2015
You can use this:
var date = new Date("2015-10-14T17:54:19.033");
var formatedDateString = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Or use moment.js
var formatedDateString = moment("2015-10-14T17:54:19.033").format('MM/dd/YYYY');
Or may you consider create your own date format function.
PS.: javascript months are 0-indexed, so for the correct month you should use (date.getMonth() + 1)

Getting the date from Javascript comes up with bizarre values

I want to get the date and time in the following format:
yyyy.mm.dd.hh.mm.ss | 2014.11.6.20.31.24
However, my code (based on Get Current Time) is instead providing these values:
y??.m?.d?.hh.mm.ss | 114.10.4.20.31.24
Here is my code:
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getYear() + "." + dt.getMonth() + "." + dt.getDay();
alert(date + "." + time);
Can someone please let me know why these odd values are in there 114.10.4 and how to change them to be what I want?
That is because you need to use
.getFullYear() for the full year
the .getMonth() is 0-based so you need to add 1
the function to get the day of month is .getDate(). The .getDay() is for the day of the week.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth()+1) + "." + dt.getDate();
alert(date + "." + time);
If, for some weird reason, you are going only for firefox, you can use
var d = new Date(),
formatted = d.toLocaleFormat('%Y.%m.%d.%H.%M.%S');
alert(formatted);
Finally, you can use the great moment.js library and do
var formatted = moment().format('YYYY.MM.DD.HH.mm.ss');
You are using the wrong getters. Use getFullYear() instead of getYear(), and getDate() instead of getDay(). And add 1 to the month, because it starts at 0.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth() + 1) + "." + dt.getDate();
alert(date + "." + time);
Just make sure that you are using methods what you want to use e.g:
dt.getYear() => dt.getFullYear()
For further reference see this.
should use getFullYear() instead of getYear() and getMonth() + 1 instead of getMonth() because it calculate form 0..11 and info about getDay()
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + dt.getMonth() + 1 + "." + dt.getDate();
alert(date + "." + time);
dt.getDay() this day of the week
The getDay() method returns the day of the week (from 0 to 6)
You need to use getDate() to know the number of the day (from 1 to 31)
Also, you need to add 1 to getMonth() because months in JavaScript starts on 0

how to change the date format using jquery

I have a input date field whose value is in this "2014-06-07 07:14"(year-month-date hour:minute) format how can i change it to 06/07/2014 07/14( mm/dd/yy) format using jquery.
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyt hh/mm");
Date date = originalFormat.parse("2014-06-07 07:14");
String formattedDate = targetFormat.format(date); // 06/07/2014 07/14
Docs of SimpleDateFormat#format
PS: A JavaScript implementation of the format() method of Java's SimpleDateFormat: is available here
Why using jQuery to do this ?
Use plain js:
var date = new Date('2014-06-07 07:14');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + '/' + date.getMinutes());
http://jsfiddle.net/F4nq8/
It seems that IE and Safari have some bug with YYYYMMDD dates so a workaround could be something like:
var s = "2014-06-07 07:14";
var t = s.split(" ");
var d = t[0].split("-");
var x = d[1] + "/" + d[2] + "/" + d[0] + " " + t[1];
var date = new Date(x);
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + '/' + date.getMinutes());
http://jsfiddle.net/F4nq8/4/
Some reference about this behaviour: http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari

How to parse Date from JSON

I am using a grid view which is getting bind by json also on some conditions and a coloum of grid contains date , so while getting data from json, I need to parse the date. I am able to get date but not time part . Tried and searched too much . I am mentioning below two methods that I tried but not solves my problem.
{
function ParseDate(jsonDate) {
date = new Date(parseInt(String(jsonDate).substr(6)));
day = date.getDate();
month = date.getMonth() + 1;
year = date.getFullYear();
return month + "/" + day + "/" + year;
}
}
This gives me only date but I need time, so I did one more method
{
function ParseDate(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var formatted = ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2) + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + "0" + date.getSeconds();
return formatted;
}
}
but this function returns
//07/19/2013 11:38
instead of //7/19/2013 11:38:07 AM which is desired result.Please help me solving this problem. Thank You very much. Also , I need to show Am or PM that is compulsory
Try this:
function ParseDateToLocale(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var myDate = new Date(date);
var formatted = myDate.toLocaleString();
return formatted;
}
See it working here: http://jsfiddle.net/2ft3A/.
Try this one, this will help you,
function ParseDate(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var merd='';
if(date.getHours()>=12)
{
merd='PM';
}
else
{
merd='AM';
}
var formatted = ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2) + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + "0" + date.getSeconds()+ " " + merd;
return formatted;
}
dateObject.toLocaleTimeString(); (OR) date.getHours() will return 0-24hrs. Base on that value you can append AM or PM

Categories

Resources