PHP date format to JS date format [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
I'm having a hard time trying to figure this out
$datenow = date('Y-m-j H:i:s'); // 2014-08-19 17:56:13
I would like to generate the exact date format with JS, how I could do this?
This is how it should appears: 2014-08-19 19:57:59

I would go like this :)
date = new Date();
dateFormated = date.getFullYear() + '-' + (date.getMonth()+1) + '-' +
date.getDay() + ' ' + date.getHours() + ':' + date.getMinutes() + ':'
+ date.getSeconds();
http://jsfiddle.net/r5mdggc8/2/
There are no leading zeros, but you can add them by using condition on each "date item" like so:
dateItem = getDay();
if (dateItem.toString().length < 2) {
dateItem = '0' + dateItem;
}
Of course, you can make a function out of it.

Related

Add 1 month to current date in JavaScript (Can you please integrate your solution/answer to this code and show) [duplicate]

This question already has answers here:
How to add months to a date in JavaScript? [duplicate]
(3 answers)
Closed 2 years ago.
$(document).ready(function () {
//Init
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(today);
});
I'm new to JavaScript So can someone help me. using this code I can get the current date...But, how can I add one month to this date and get the next month date..
Currently I'm getting this - 12/30/2020 (Today's Date) Add 1 month Means I want to get - 01/30/2021 (Next month date).
Can you please integrate your solution/answer to this code and show
Try this
$(document).ready(function () {
//Init
var now = new Date();
// Add one month to the current date
var next_month = new Date(now.setMonth(now.getMonth() + 1));
// Manual date formatting
var day = ("0" + next_month.getDate()).slice(-2);
var month = ("0" + (next_month.getMonth() + 1)).slice(-2);
var next_month_string = next_month.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(next_month_string);
});
You can also use this trick to get your YYYY-MM-DD style string instead of the manual formatting:
var next_month_string = next_month.toISOString().split('T')[0];

convert milliseconds to date time in javascript without using moment [duplicate]

This question already has answers here:
milliseconds to time in javascript
(19 answers)
Closed 4 years ago.
How to convert time from milliseconds to date time in javascript.
I need to convert from milliseconds to below mentioned format.
format : 20-10-1994 06:00:00
You can use it that way.
var d = new Date(milliseconds);
var myDate = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
Reference:
https://www.w3schools.com/jsref/jsref_obj_date.asp
https://www.w3schools.com/js/js_date_methods.asp

How to convert UTC to IST time format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Convert date to another timezone in JavaScript
(34 answers)
Closed 5 years ago.
Input:- 2018-01-19T17:04:54.923Z;
Output:- 2018-01-19 17:04:54;
How can I get universal logic which will work in all browsers in JAVASCRIPT
You can use this:
var dt = new Date('2018-01-19T17:04:54.923Z');
var formatedString = dt.getFullYear() + "-" + dt.getMonth() + 1 + "-" + dt.getDate() + " " + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.log(formatedString);
In javascript you can convert UTC date to local date format using below snippets.
// Your input date
var utcDate = new Date('2018-01-19T17:04:54.923Z');
//Converted UTC to local(IST, etc.,,)
console.log(utcDate.toUTCString());
/*
Output is : "Fri, 19 Jan 2018 17:04:54 GMT"
*/

get date as timestamp using javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
$a = date('Y-m-d h:i:s');
echo $a;
result:
2017-08-18 09:14:02
Is there a way to get the date in the same format using javascript / jquery ?
Here's an approach that is easy to understand for newer JavaScript developers who come from an OOP background.
var date = new Date();
alert(date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + " " + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2) + ":" + ("0" + date.getSeconds()).slice(-2));
date.getMonth() + 1 because the months are 0 indexed.
EDIT: The above solution now adds the leading zeros to getMonth() and getDay(). The slice(-2) call is a common way to getting the last two characters from the string.
For example, if date.getMonth() returns a 9. I would get 09, and slice(-2) would return me the same 09.
But if date.getMonth() returns a 10. I would get 010, and slice(-2) would return the last two characters again. So, 10.
The other answers are correct, this one is just easier to understand from a beginners perspective.
Try this
var date = new Date();
var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/)
var correctdate = iso[1] + ' ' + iso[2];
var DateWithTime = new Date();
console.log(DateWithTime); //Date 2017-08-18T17:29:34.660Z
var onlyDate = DateWithTime.toISOString().substring(0, 10);
console.log(onlyDate); //2017-08-18

JavaScript format date like in PHP [duplicate]

This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 9 years ago.
I need to parse date from JSON (I can't do any change in this JSON on server).
{ ...
"time":"2014-02-14 18:37:48",
...
}
In php date() it is: YYYY-mm-dd HH:ii:ss
I want to change date format, for example to "dd.mm.YYYY HH:ii". In PHP it is easy, but in JavaScript I do not know how to parse it.
I try jQuery dateFormat, but I still do an error :-(
Can you please help me?
var arr=time.split(' ');
var date_arr=arr[0];
var time_arr=arr[1];
var temp_date=date_arr.split('-');
var temp_time=time_arr.split(':');
var js_date=temp_date[2]+'.'+temp_date[1]+'.'+temp_date[0]+' '+temp_time[0]+":"+temp_time[1];
You need to do all by your hands. Javascript's Date object has enough methods.
So please try smth like this:
var dateTime = new Date(Date.parse("2014-02-14 18:37:48"));
var date = dateTime.getDate().toString().length > 1 ? dateTime.getDate() : '0' + dateTime.getDate();
var month = dateTime.getMonth().toString().length > 1 ? dateTime.getMonth() + 1 : '0' + (dateTime.getMonth() + 1);
var hours = dateTime.getHours().toString().length > 1 ? dateTime.getHours() : '0' + dateTime.getHours();
var minutes = dateTime.getMinutes().toString().length > 1 ? dateTime.getMinutes() : '0' + dateTime.getMinutes();
var formattedDate = date + '.' + month + '.' + dateTime.getFullYear() + ' ' + hours + ':' + minutes;
console.log(formattedDate);

Categories

Resources