Insert character in string using javascript [duplicate] - javascript

This question already has answers here:
Insert a string at a specific index
(19 answers)
Closed 6 years ago.
I have this date1, I want to insert "-" to make it 2016-09-23. Is anyone know how to do this using javascript?
var date1 = "20160923";

You can use regex:
var ret = "20160923".replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3");
console.log(ret);
/)

Given that the year is 4 digit and month and day are 2 digit you can use this code
var date1 = "20160923";
var formattedDate = date1.slice(0, 4) + "-" + date1.slice(4, 6) + "-" + date1.slice(6, 8);
console.log(formattedDate);

There is no direct method for this, you can write your own Method like InsertAt(char,pos) using Prototype object [References from here]
String.prototype.InsertAt=function(CharToInsert,Position){
return this.slice(0,Position) + CharToInsert + this.slice(Position)
}
Then use it like this
"20160923".InsertAt('-',4); //Output :"2016-0923"

Assuming date1 is always consistent...
var date2 = date1.slice(0, 4) + '-' + date1.slice(4, 6) + '-' + date1.slice(6, 8);

Related

Convert json string to date in dd-mm-yy format using javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I want to bind data in a dd-mm-yy format for which I am using json to bind. Currently I get date as 2014-06-18T00:00:00
I want in dd-mm-yy format. Kindly let me know how to do that.
Below is my code for the same.
if (getJSONValue.LAUNCH_DATE != "" || getJSONValue.LAUNCH_DATE == null) {
$('#txtLaunchDate').val(getJSONValue.LAUNCH_DATE);
}
my getJSONValue.LAUNCH_DATE = 2014-06-18T00:00:00
see snippet
var newDate = new Date("2014-06-18T00:00:00");
var day = newDate.getDate();
var month = newDate.getUTCMonth() + 1;
var year = newDate.getFullYear();
console.log(day + "-" + ("0" + (month)) + "-" + year );
Using momentjs:
const date = '2014-06-18T00:00:00'
const format = 'DD-MM-YY'
console.log(moment(date).format(format))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>

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

Convert date in JavaScript [duplicate]

This question already has answers here:
Reformat string containing date with Javascript
(3 answers)
Closed 5 years ago.
I need to convert a data from this format 26/03/2017 in this format 2017-03-26, starting from picking data from a form id.
I've tried like this, but I'm lost now.. any help?
var dataform = "26/03/2017";
var dataora = new Date(dataform);
var G = dataora.getDate(dataform);
var M = (dataora.getMonth(dataform) + 1);
if (G < 10)
{
var mm = "0" + dataora.getDate(dataform);
}
else
{
var mm = dataora.getDate(dataform);
}
if (M < 10)
{
var gg = "0" + (dataora.getMonth(dataform) + 1);
}
else
{
var gg = (dataora.getMonth(dataform) + 1);
}
var aa = dataora.getFullYear(dataform);
var data = aa + "-" + mm + "-" + gg;
console.log(data);
console.log("Year "+aa);
console.log("Month "+mm);
console.log("Day "+gg);
Output is:
2019-03-02
Year 2019
Month 03
Day 02
Where am I wrong?
Split the date using split function with /.
Reverse it using the reverse function
Then Join it using the join function with -.
That's it.
var date="26/03/2017";
date=date.split("/").reverse().join("-");
console.log(date);
Where am I wrong?
Don't use the Date constructor (or Date.parse) to parse strings, as it's largely implementation dependent and varies across hosts. See Why does Date.parse give incorrect results?
Per Sagar V's answer, just reformat the string.

PHP date format to JS date format [duplicate]

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.

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